13

When I open the app from a deeplink (user clicks on URL) and press back button I expect user to navigate to a previous fragment in my navigation graph but it just exits the app.

The documentation says that back navigation should work the same way as if it the user got to that screen naturally.

Can I somehow specify the desired backstack in my navigation graph? Or can be backstack formed automatically after a deeplink? For older version of the library I found out that after back press it should navigate to the root of my navigation graph but that does not happen.

I’m using the navigation library from Android architecture components (version 1.0.0-beta01).

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
xPeep
  • 131
  • 1
  • 4
  • I'm experiencing the same issue, my use case is that I trigger the `handleDeeplink` method from the app itself in some cases which should land in different destinations based on the give url but this method clears the backstack when it goes to the destination. As you mentioned it is a change introduced in the new versions of the nav component. Did you find any thing around it or reached the google team? – Kayvan N Mar 06 '19 at 21:33

2 Answers2

7

I found out what's going on here, for explicit deep links its supposed to go to a new back stack which is what you app would have if a user had naturally navigated to the view not the existing back stack (existing stack is cleared.

When a user opens your app via an explicit deep link, the task back stack is cleared and replaced with the deep link destination. When nesting graphs, the start destination from each level of nesting—that is, the start destination from each element in the hierarchy—is also added to the stack. This means that when a user presses the Back button from a deep link destination, they navigate back up the navigation stack just as though they entered your app from its entry point.

For implicit its a bit strange. You can make it do what explicit does but setting Intent.FLAG_ACTIVITY_NEW_TASK otherwise the back button and the navigation up button do two separate things:

  1. The back button will do as you might expect, it will go back in your apps existing back stack and load that fragment.

  2. The up button however will clear the a back stack and make a new one as if it was an explicit link.

If the flag is not set, you remain on the task stack of the previous app where the implicit deep link was triggered. In this case, the Back button takes you back to the previous app, while the Up button starts your app's task on the hierarchical parent destination within your navigation graph.kquote

Source: Android Documentation

MobDev
  • 1,219
  • 1
  • 12
  • 25
  • Can you help me with this: https://stackoverflow.com/questions/64687574/android-navigation-url-deep-link-back-to-previous-app/64740896#64740896, thank you. – Sam Chen Nov 10 '20 at 04:57
-1

As describe here back button should return to the previous fragment, you can set it manually in Java like this: button.setOnClickListener(Navigation.createNavigateOnClickListener(R.id.next_fragment, null));

In Kotlin like that: button.setOnClickListener(Navigation.createNavigateOnClickListener(R.id.next_fragment, null))

The Android system maintains a back stack containing the last visited destination. The first destination of your app is placed on the stack when the user opens the app. Each call to the navigate() method puts another destination on the top of the stack. Conversely, pressing the Up or Back button calls the NavController.navigateUp() and NavController.popBackStack() methods, respectively, to pop the top destination off of the stack.

Make sure that you are using NavHostFragment and not <fragment> in your hosting fragment activity.

Guy Luz
  • 3,372
  • 20
  • 43