Creating back stack with Android Navigation Component - android

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.

Related

I want to keep AddTransactionFragment while I navigate to other fragment with BottomNavigation (Android, Kotlin)

My problem is illustrated by this screen record.
When I click the FAB on FinanceFragment, it will open AddTransactionFragment. But when I navigate to ReminderFragment and go back to FinanceFragment, the AddTransactionFragment is destroyed.
How could I -- similar to the YouTube app when one switches between fragments -- keep AddTransactionFragment visible while I navigate to another fragment?
I tried to add addToBackStack() but this turned out not to be what I was looking for.

Android Navigate Component navigate to sub graph

I hava two Fragment(OverviewFragment、PersonalFragment), and one sub graph(contains two Fragment :BucketsFragment and ObjectsFragment, BucketsFragment is start destination). They are bind to one BottomNavigateView.
Now, After navigate to the sub graph start destination(BucketsFragment), and continue to navigate to ObjectsFragment. Then navigate to OverviewFragment and finally return to the sub graph.
Now, I came to start destination(BucketsFragment), but actually what I want is ObjectsFragment. what should I do?
The whole process is shown here
I want to navigate to the sub graph that retains the previous state, instead of a new sub graph.
As per the material design guidelines for bottom navigation:
On Android: the app navigates to a destination’s top-level screen. Any prior user interactions and temporary screen states are reset, such as scroll position, tab selection, and in-line search.
So technically, this behavior is expected.
When you're on the Resources tab and go to the ObjectsFragment, the back stack is
DashBoardFragment -> BucketsFragment -> ObjectsFragment
When you go back to the Overview tab, the back stack becomes
DashBoardFragment
And no state is saved for Fragments that aren't on the back stack. That's why when you reselect the Resources tab, it is recrated from scratch and you get back to
DashBoardFragment -> BucketsFragment
There's an existing issue for supporting multiple back stacks which aims to bring support for saving the state of each tab separately, allowing the behavior you wish. As per that issue, this requires significant changes to how Fragments work (since they are the one saving the state of Fragments) as well as integration into Navigation itself.
That issue points out a temporary workaround, demonstrated in the NavigationAdvancedSample where each tab uses its own separate navigation graph and separate NavHostFragment, thus allowing each one to keep its own state independently from one another. This requires a bit different of a setup in the MainActivity and the help of a set of NavigationExtensions to get that working.
It is expected that all of that functionality, once the multiple back stacks work is done, to be folded into the Navigation library itself.

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.

Android Navigation Editor - Finish Activity?

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!).

Categories

Resources