Back navigation after deep link by navigation architecture component - android

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.

As mentioned in the documentation :
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.
the start destination of the graph also added to the stack.
So when you click on the back button, the start destination on the graph coming to the top of the stack.

Related

How to exit feature's navigation graph?

I have a feature as a module which consist of 3 fragments.
After User navigates to the 3'd fragment, there is a computation, when computation succeeds -> User should be navigated to the Home screen which is in the parent nav graph.
I've tried a lot of approaches, but nothing seem to work correctly, is there a way to finish current navigation graph and return to the parent graph?
Things I've tried:
Navigation to the home with a help of deeplink. Not a bad option, but it messes with backStack, and it is not user friendly in case feature will be exposed to different clients.
Navigating to the start destination with cleaning of the backstack and after navigateUp. Better option than the first, but still...

Back stack is not getting cleared when navigating between activities using Navigation Component

I've two navigation graphs, one for login/sign-up(login_nav_graph) screens and one for home(home_nav_graph) screens. Each navigation graph has one activity and couple of fragments.
First I'm launching home_nav_graph, and checking if the user logged in or not. If the user not logged-in, I'm navigating to the login_nav_graph using its activity as the destination. And once the user is logged in successfully, I'm navigating to the home_nav_graph using the activity inside this graph as the destination.
Problem: When I navigate between these two navigation graphs, the back stack is not getting cleared. When I press the back button in login_nav_graph it's taking back to the screens in home_nav_graph.
I've already used the action elements like popUpTo, popToInclusive when navigating to the other graph but still back stack is not getting cleared.
Navigation component version I'm using: 2.3.0-alpha06
Update:
This is the sample project I've created to reproduce the issue - test-navigation.
Also, created an issue tracker for this problem.

Add a Fragment to Backstack of Navigation Graph

I am using the UI Navigation Graph. I have fragment A and B. My application starts on Fragment B to show some information. Then the user can press back and end up in A. Then from A the user usually picks something and ends up in B.
Normally I can pop B from the stack and show A. However, when the app just launches there is no A in the back stack. I guess I could add A to the backstack manually but I wonder if the Navigation UI Graph has something that lets me do this.
I cannot change the order of my application fragments. That means that I still need to show B first.
As per the Principles of Navigation, the start destination of your app should always be the last screen the user sees before they exit your app. That means that your Fragment A should be the start destination of your graph.
As per the Conditional Navigation documentation, if you have important information that the user should see (such as a first time user experience), you should have your Fragment A navigate to your Fragment B. This ensures that when you're on Fragment B, Fragment A is on the back stack already.

Creating back stack with Android Navigation Component

I am having an issue creating a back stack with the new android navigation component. I have a main graph with a splash fragment, a loginregister root fragment that has it's own graph, an onboard step 1 fragment with corresponding step 1 graph, step2 fragment with step 2 graph, and a home fragment. When the user logs in, they can go to either step1, step2, or the home. If they go to step2, I want them to be able to hit back and end up at step1, and even better, the last fragment of the step1 graph. I've tried setting global actions and setting popTo, but I often get an error saying that it's ignoring the popTo because step1 isn't part of the back stack. Having a button on the screen to go back so I can call navigate with global action works, but when using the hard back button it will go from step2 to login/register.
Even just a simple case of one graph, fragments A, B, C, D. The user can enter the app at any fragment. If I just try to navigate to fragment C with the BtoC action, then there is an error of this action is unknown to this controller. Currently I have to essentially make a couple navigate calls, one after another.
In on of the navigation training docs it says "And in both cases, you should make the Back button more predictable by inserting into the task's back stack the complete upward navigation path to the app's topmost screen. " But I can't find anywhere that really shows how to do this with the NavController and single activity apps.
Any insight would be appreciated.

Android Navigation library deep linking: How to synthesise backstack

Using Android Architecture's Navigation component, I have the following navigation graph
-> [Home] -> [Articles List] -> [Specific Article]
I also have a deeplink to [Specific Article]. When it is opened, navigating up currently goes to [Home].
I'd like to synthesise a backstack such that navigating up instead goes back to [Articles List] (and then on to [Home] if navigating again).
What is the Navigation way of doing this?
Per the NavDeepLinkBuilder documentation, Navigation uses the startDestination of the destination for the synthetic back stack. If you Group destinations into a nested navigation graph, both the startDestination of the nested graph and the startDestination of the root graph are added to the back stack. This gives you the ability to have [Articles List] as the startDestination of the nested graph to add it to your back stack.
However, it is strongly recommended to keep your synthetic back stack as small as possible - while a depth of 2 or 3 (as here) is fine, it is not recommended to go much beyond that level to avoid cases where users have to repeatedly tap and tap the back button to get back to the launcher.
The documentation implies that my original solution should work.
When a user uses the Back button from a deep link destination, they navigate back up the navigation stack just as though they entered your app from the app’s entry point.
In addition, ianhanniballake's answer doesn't produce expected results (the deeplinked fragment is not opened).
I have created an issue on google's tracker for both these problems: https://issuetracker.google.com/issues/79734195
I came across this thread while using navigation compose. The issue for me was that I called navController.popBackStack() instead of the correct navController.navigateUp(). After changing my calls to navigateUp(), everything works as expected. Maybe this helps someone with the same problem.

Categories

Resources