Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing argument(s) to a nested Navigation architecture component graph

How does one pass argument(s) to a nested Navigation architecture component graph?

Let's say I construct my navigation graph to navigate from FragmentA --> Nested, where Nested contains FragmentB --> FragmentC...

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...

So what am I supposed to do?

like image 482
Algar Avatar asked Feb 01 '19 10:02

Algar


People also ask

How do you pass an argument in a NAV graph?

In the Navigation editor, click on the destination that receives the argument. In the Attributes panel, click Add (+). In the Add Argument Link window that appears, enter the argument name, argument type, whether the argument is nullable, and a default value, if needed. Click Add.


2 Answers

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.

like image 75
Algar Avatar answered Sep 19 '22 21:09

Algar


actully it's very simple you just need to add your arguments in nested_nav for example

 <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> 

or you can send your data with a bundle

like image 30
ilyas ipek Avatar answered Sep 18 '22 21:09

ilyas ipek