1

Here is the code for app_nav_graph.xml with a nested nav graph:

<navigation 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/app_nav_graph"
    app:startDestination="@id/homeFragment">

    <include app:graph="@navigation/nested_nav_graph" />

    <fragment
        android:id="@+id/homeFragment"
        android:name="com.sd.android.ui.HomeFragment">
        <action
            android:id="@+id/action_homeFragment_to_nestedNavGraph"
            app:destination="@id/nested_nav_graph" />
    </fragment>
</navigation>

Here is nested_nav_graph.xml:

<navigation 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/nested_nav_graph"
    app:startDestination="@id/nestedHomeFragment">
    <fragment
        android:id="@+id/nestedHomeFragment"
        android:name="com.sd.android.ui.NestedHomeFragment">
    </fragment>
</navigation>

And here is the navigation code placed inside HomeActivity (hosting NavHostFragment):

 navController.setGraph(R.navigation.app_nav_graph);
 Bundle bundle = new Bundle();
 bundle.putInt("key", 1);
 navController.navigate(R.id.action_homeFragment_to_nestedNavGraph);

The issue is that NestedHomeFragment is not receiving the passed bundle i.e. getArguments() is returning null.

Why is this happening? Any suggestions to fix this?

Thanks

Wamiq
  • 1,114
  • 3
  • 12
  • 23
  • What version of Navigation are you using? Is there a reason you're doing all this and not just [passing arguments to your start destination directly](https://developer.android.com/guide/navigation/navigation-pass-data#start)? – ianhanniballake Jul 11 '21 at 18:15
  • This is code for navigation is executed after clicking on the navigation drawer item. Navigation version is 2.3.5. – Wamiq Jul 11 '21 at 18:23
  • I'm a bit confused. You're calling all that code, including `setGraph`, as part of clicking on a navigation drawer item? Can you explain why you're doing that with something more realistic than `putInt("key", 1)` as this isn't really a pattern you should be doing at all. – ianhanniballake Jul 11 '21 at 18:49
  • But even though the pattern is correct or not (which I can try to explain and there might be better ways to achieve the navigation behaviour), the bundle should be ideally be passed to the startDestination of the nested nav graph? Is that expectation incorrect here? – Wamiq Jul 11 '21 at 18:55

2 Answers2

0

In your code, you are creating your Bundle, but you aren't actually passing it to your call to navigate(). You need to do that for it to actually be sent onto your destination:

Bundle bundle = new Bundle();
bundle.putInt("key", 1);
navController.navigate(R.id.action_homeFragment_to_nestedNavGraph, bundle);
ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
0

To resolve this issue, please check the answer I posted in another thread.

Basically, it has two approaches. The first one is more reliable due to the NavigationComponent and SafeArgs scoping. The second approach is more custom via using the extra NavDirections class.

GIGAMOLE
  • 1,274
  • 1
  • 11
  • 17