将参数传递给嵌套的导航架构组件图
Passing argument(s) to a nested Navigation architecture component graph
如何将参数传递给嵌套的导航架构组件图?
假设我构建导航图从 FragmentA --> Nested
开始导航,其中 Nested
包含 FragmentB --> FragmentC
...
如果这是一个纯粹的 FragmentA --> FragmentB...
图,我会用 FragmentADirections.actionFragmentAToFragmentB(argument = foo)
设置导航。但是,一旦您将 B --> C
变为 Nested
...
,该操作就会采用零参数
那我该怎么办?
全局操作可能是一种方式,但一旦我将嵌套图提取到它自己的 .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>
换句话说,只需将相同的 <argument ... />
添加到 root_graph
操作中,就像您希望在 nested_graph
中收到的那样。
如果您不想为嵌套图创建单独的 xml,您可以作为导航组件的 android 开发人员 says here. I just test it for use with navigation graph view model scope and it worked perfectly. I am using 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>
其实很简单,你只需要在 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>
或者您可以使用捆绑包发送您的数据
如何将参数传递给嵌套的导航架构组件图?
假设我构建导航图从 FragmentA --> Nested
开始导航,其中 Nested
包含 FragmentB --> FragmentC
...
如果这是一个纯粹的 FragmentA --> FragmentB...
图,我会用 FragmentADirections.actionFragmentAToFragmentB(argument = foo)
设置导航。但是,一旦您将 B --> C
变为 Nested
...
那我该怎么办?
全局操作可能是一种方式,但一旦我将嵌套图提取到它自己的 .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>
换句话说,只需将相同的 <argument ... />
添加到 root_graph
操作中,就像您希望在 nested_graph
中收到的那样。
如果您不想为嵌套图创建单独的 xml,您可以作为导航组件的 android 开发人员 says here. I just test it for use with navigation graph view model scope and it worked perfectly. I am using 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>
其实很简单,你只需要在 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>
或者您可以使用捆绑包发送您的数据