Michał Ziobro
Michał Ziobro

Reputation: 11762

Multiple Navigation Graphs in Android and Deep Links

I have 3 Activities: Main, Login, Wizard.

For each Activity, I have separate Navigation Graph with fragment destinations.

Main Nav Graph has also a Login Activity destination to launch the Login screen on logout.

The app seems to work correctly when launched from Action.MAIN launcher intent.

However, I experience problems when using Deep Link. I would like to add a URI to the Login Nav Graph (Change Password Fragment). This Nav Graph uses a separate NavHostFragment. I have used 2 approaches.

  1. Directly add a Deep Link to Change the Password Fragment Destination in Login Nav Graph -> Deep Links navigate only to START DESTINATION (Login Fragment Destination). Here also Back button does NOT work correctly, i.e. finishing the app instead of returning to Main Nav Graph Main Destination.
  2. Add Deep Link to Login Activity Destination in Main Nav Graph again and here Back button does work correctly, I as expected landed on Login Fragment Destination. But here I want to manually navigate to Change Password Fragment Destination and I experience another problem, i.e. the Intent Action.View with Uri data is not delivered into this Login Activity destination. It is only delivered to the Main Activity which hosts Main Nav Graph.

To sum up. I think this Deep link behavior between multiple Nav Graphs connected by Activity Destinations does NOT work as it should. I do not know whether there are any solutions to this.

Can I retrieve Deep Link Arguments (Uri data) from NavController somehow? Or the only solution is to get it from getIntent().data? Here as I say this Intent is not forwarded to the final destination Activity, but only to the first Activity in the created stack of Activities.

Now I returned to manually handling Deep Links without Android Navigation Architecture as it seems useless if there is a more complex navigation structure than a single Nav Graph with a single Activity and only inner Fragment navigation.

Upvotes: 48

Views: 3783

Answers (1)

Annas Surdyanto
Annas Surdyanto

Reputation: 113

You should use only one main graph to include your graphs as written in the docs.

And related to unexpected back press result, use NavDeepLinkRequest.

val request = NavDeepLinkRequest.Builder
    .fromUri("android-app://example.google.app/settings_fragment_two".toUri())
    .build()
findNavController().navigate(request)

As stated in the docs,

Unlike navigating using action or destination IDs, you can navigate to any URI in any graph, even across modules.

When navigating using URI, the back stack is not reset. This behavior is unlike explicit deep link navigation, where the back stack is replaced when navigating.

To navigate among modules, make sure you use the main navController (of the main graph). You can call it from other modules by tag anyway.

Upvotes: 2

Related Questions