将参数传递给嵌套的导航体系结构组件图 [英] Passing argument(s) to a nested Navigation architecture component graph
问题描述
一个参数如何传递给嵌套的导航体系结构组件图?
How does one pass argument(s) to a nested Navigation architecture component graph?
假设我构造了导航图以从FragmentA --> Nested
导航,其中Nested
包含FragmentB --> FragmentC
...
Let's say I construct my navigation graph to navigate from FragmentA --> Nested
, where Nested
contains FragmentB --> FragmentC
...
如果这是一个纯FragmentA --> FragmentB...
图,我将使用FragmentADirections.actionFragmentAToFragmentB(argument = foo)
设置导航.但是,只要您将B --> C
转换为Nested
...
If this was a pure FragmentA --> FragmentB...
graph, I would just set up the navigation with FragmentADirections.actionFragmentAToFragmentB(argument = foo)
. But that action takes zero arguments as soon as you turn B --> C
into Nested
...
那我该怎么办?
推荐答案
全局动作也许是一种方法,但是一旦将嵌套图提取到自己的.xml
中,我就无法按需工作.但这真是令人尴尬的简单-只需在代码中手动将参数添加到您的操作中即可.
Global actions might be a way but I didn't get that working as I wanted once I extracted the nested graph to its own .xml
. But it turned out to be embarrassing simple - just add the arguments manually in code, to your action.
与该问题有关的示例为:
An example related to the question would be:
将嵌套图保存到nested_graph.xml
,它将类似于
Save the nested graph to nested_graph.xml
, it will look something like
<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
做
To pass arguments to nested_graph.xml
from a different graph, say 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 ... />
相同的<argument ... />
添加到root_graph
动作中即可.
In other words, just add the same <argument ... />
to the root_graph
action as you expect to receive in the nested_graph
.
这篇关于将参数传递给嵌套的导航体系结构组件图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!