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:
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>
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>
In other words, just add the same <argument ... />
to the root_graph
action as you expect to receive in the nested_graph
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…