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.
Related
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...
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.
Currently in my navgraph i have dialog fragment destination ( Fragment(dialog) type). When i'm navigating to this dialog and then try to navigate to another fragment from this dialog destination, dialog is closing which in my opinion is upredictable behaviour.
Now i'm only navigating to the next fragment like this.
findNavController().popBackStack(R.id.testFragment, true)
I want to dialog not closing and only navigate to another dialog like a default fragment. How can i achieve this ?
As per this issue, this is working as intended:
There's a couple of problems here relating to how Dialogs and Fragments work:
Dialogs are separate windows that always sit above your activity's window. This means that the dialog will continue to intercept the system back button no matter what state the underlying FragmentManager is in or what FragmentTransactions you do.
Operations on the fragment container (i.e., your normal <fragment> destinations) don't affect dialog fragments. Same if you do FragmentTransactions on a nested FragmentManager.
The initial release of Navigation's <dialog> support did not take these limitations into account and would treat dialog destinations just like any other in that, from Navigation's point of view, they could be put on the back stack and treated like any other <fragment> destination.
As that's not actually the case, we've made a few changes for Navigation 2.1.0-alpha06 to ensure that Navigation's state of the world matches what you actually see on the screen and prevent crashes like that in comment#5.
The summary of this is that <dialog> destinations are now automatically popped when navigate to a non-dialog and non-activity destination, such as a destination. For reference, this was done in https://android-review.googlesource.com/996359 and https://android-review.googlesource.com/1007662
So it is expected when you navigate to a non-dialog destination that any dialog destination is popped off the back stack.
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.
I'm trying out Android's new Navigation Editor for the first time and I'm not sure if this is a missing feature, intentional omission, or if I'm missing something. I have two fragments and I want the first fragment to be able to navigate to the second one, but I want the activity to finish if back is pressed from either fragment.
With my current setup, I can navigate from mainFragment to newFragment. If I press back from the mainFragment, the activity finishes. The only piece I can't figure out is how to finish the activity when back is pressed from newFragment. I've tried every combination of Pop Behavior settings, but haven't achieved what I'm looking for.
Just set clearTask to "true" on your action.
But your use case is going against the concept of the navigation.
https://developer.android.com/topic/libraries/architecture/navigation/navigation-principles#the_app_should_have_a_fixed_starting_destination
Apps have a fixed destination which is the screen the user sees when they launch your app from the launcher. This destination should also be the last screen the user sees when they return to the launcher after pressing the back button.
See the screenshot and look for Pop Behavior. This option can be used to finish activity.
Please note: Finish activity = pop the Activity off the stack.
Select the action from the Activity to be finished, in navigation graph.
Look for drop down for Pop To.
Select the fragment(i.e. the navHostFragment of the activity to be finished).
Check Inclusive option. (i.e. From current destination point- in ur case, it's an action - to and including this fragment - in ur case navHostFragment of Activity- in the stack will be popped off the stack. And that's what we need!).