【问题标题】:Passing argument(s) to a nested Navigation architecture component graph将参数传递给嵌套的导航架构组件图
【发布时间】:2020-12-03 02:18:44
【问题描述】:

如何将参数传递给嵌套的导航架构组件图?

假设我构建导航图以从FragmentA --> Nested 导航,其中Nested 包含FragmentB --> FragmentC...

如果这是一个纯粹的FragmentA --> FragmentB... 图表,我会使用FragmentADirections.actionFragmentAToFragmentB(argument = foo) 设置导航。但是,只要您将B --> C 转换为Nested,该操作就需要零参数...

那我该怎么办?

【问题讨论】:

    标签: android android-architecture-components android-architecture-navigation


    【解决方案1】:

    全局操作可能是一种方法,但是一旦我将嵌套图提取到它自己的.xml 中,我并没有按照我想要的方式进行操作。但事实证明这很简单 - 只需在代码中手动将参数添加到您的操作中。

    与该问题相关的示例是:

    将嵌套图保存到nested_graph.xml,它看起来像

    <navigation
        android:id="@+id/nested_graph"
        app:startDestination="@id/fragmentB"
        ...>
    
        <fragment 
            android:id="@+id/fragmentB"
            ...>
            <argument
                android:name="foo"
                app:argType="integer"/>
            <action 
                ... // navigation action to FragmentC />
        </fragment>
    
        <fragment ...  // FragmentC stuff
    </navigation>
    

    要从不同的图表将参数传递给nested_graph.xml,比如root_graph.xml do

    <navigation
        android:id="@+id/root_graph"
        app:startDestination="@id/fragmentA"
        ...>
    
        <fragment 
            android:id="@+id/fragmentA"
            ... >
            <action
                android:id="@+id/action_fragmentA_to_nested_graph"
                app:destination="@id/nested_graph">
                <argument
                    android:name="foo"
                    app:argType="integer"/>
            </action>
        </fragment>
        <include app:graph="@navigation/nested_graph"/>
    </navigation>
    

    换句话说,只需将相同的&lt;argument ... /&gt; 添加到root_graph 操作中,就像您希望在nested_graph 中收到的一样。

    【讨论】:

    • 这个解决方案对我有用。你有一个小错字:应该是 app:argType 而不是 android:argType
    • @RomanTikhonov 感谢您的更正!!我已经更新了答案:)
    • 同意这似乎是目前唯一的解决方法。有一个改进的功能请求:issuetracker.google.com/issues/109505019
    • 非常容易 :)) 他们是那些在属性编辑器窗口中禁用它的人:/
    • 导航编辑器中没有记录/或提供此功能是否有合理的原因?我的意思是,是否假设我们会存储需要在共享视图模型或类似的东西中传递的值?我们需要一个 SO 帖子来解释这个简单的功能,这似乎很尴尬。
    【解决方案2】:

    其实很简单,你只需要在nested_nav 中添加你的参数 例如

     <navigation
            android:id="@+id/root_graph"
            app:startDestination="@id/fragmentA"
                ...>
    
                <fragment
                    android:id="@+id/fragmentA"
                ... >
                <action
                    android:id="@+id/action_fragmentA_to_nested_graph"
                    app:destination="@id/nested_graph">
                </action>
            </fragment>
            <navigation
                android:id="@+id/nested_graph"
                app:startDestination="@id/fragmentB"
                    ...>
                <argument
                    android:name="foo"
                    app:argType="integer"/>
                <fragment
                android:id="@+id/fragmentB"
                    ...>
                <argument
                android:name="foo"
                app:argType="integer"/>
                <action
                    ... // navigation action to FragmentC />
                    </fragment>
                
                <fragment ...  // FragmentC stuff
            </navigation>
        </navigation>
    

    或者您可以使用捆绑包发送数据

    【讨论】:

      【解决方案3】:

      如果您不想为嵌套图创建单独的 xml,您可以以 Android 开发人员says here 的身份在操作中覆盖目标参数。我只是测试它以与导航图视图模型范围一起使用,它工作得很好。我正在使用导航组件的Version 2.2.0-alpha03。将这些参数添加到动作 action_inboxFragment_to_conversation_graph 后,现在 InboxFragmentDirections.ActionInboxFragmentToConversationGraph 已正确生成。

      <?xml version="1.0" encoding="utf-8"?>
      <navigation xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          xmlns:tools="http://schemas.android.com/tools"
          android:id="@+id/messages_graph"
          app:startDestination="@id/inboxFragment">
          <fragment
              android:id="@+id/inboxFragment"
              android:name="com.wlpr.docfinder.ui.fragment.InboxFragment"
              android:label="fragment_inbox"
              tools:layout="@layout/fragment_inbox" >
              <action
                  android:id="@+id/action_inboxFragment_to_conversation_graph"
                  app:destination="@id/conversation_graph">
                  <argument
                      android:name="participantName"
                      android:defaultValue="Conversation"
                      app:argType="string"
                      app:nullable="true" />
                  <argument
                      android:name="documentsCount"
                      android:defaultValue="1"
                      app:argType="integer" />
              </action>
          </fragment>
          <navigation
              android:id="@+id/conversation_graph"
              android:label="conversationGraph"
              app:startDestination="@id/conversationFragment">
              <fragment
                  android:id="@+id/conversationFragment"
                  android:name="com.wlpr.docfinder.ui.fragment.ConversationFragment"
                  android:label="fragment_conversation"
                  tools:layout="@layout/fragment_conversation">
                  <action
                      android:id="@+id/action_conversationFragment_to_reportingDetailsFragment"
                      app:destination="@id/reportingDetailsFragment" />
                  <argument
                      android:name="participantName"
                      android:defaultValue="Conversation"
                      app:argType="string"
                      app:nullable="true" />
                  <argument
                      android:name="documentsCount"
                      android:defaultValue="1"
                      app:argType="integer" />
              </fragment>
              <!-------- more fragments... -------->
      </navigation>
      

      【讨论】:

      • 你在这里覆盖什么?
      猜你喜欢
      • 2018-10-24
      • 1970-01-01
      • 1970-01-01
      • 2020-08-06
      • 1970-01-01
      • 2022-11-11
      • 1970-01-01
      • 2021-12-02
      • 2019-05-23
      相关资源
      最近更新 更多