Fragment navigation in menu - android

I have a common layout where I have a menu in a DrawerLayout and these menu items replace the visible main fragment in my Activity. The top menu item represents my Home screen(fragment A). When I press something else(fragment B) in the menu I replace A with B and add the transaction to the back stack (A -> B). If I select fragment C from the menu I would like the back stack now to be A -> C. In order to do that I call popBackStack() and then replace A with C.
My issue here is that I would like the transition between B and C to be smooth but when I call popBackStack(), fragment A is visible for a split second before it's replaced with C. So what I want is basically creating a FragmentTransaction where I can pop the back stack without committing it before I replace A with C.
To clarify: I should note that when you are in fragment B you could select something there that would take you deeper so the stack would then be A -> B -> B1. If I press C from the menu at this point I need to clear the backstack and show C WITHOUT flashing A in the process.
Does anyone know if that is possible or does anybody have any other solution to my issue ?

Don't add any of fragment in back stack. Just replace one with another. In this way we will skip to call popBackStack().

Not sure if you've solved this issue, but I have been working on a library that should help you solve your navigation issues!
Since the implementation of the ChildFragmentManager it has given us flexibility in creating back stacks within individual fragments. I am leveraging this in the library. It's accessible in jcenter repository, please see the ReadMe for implementation details!
https://github.com/DMCApps/NavigationFragment
Basically you will need to use a SingleStackNavigationManagerFragment as your base fragment. When the menu item from A->B is clicked, call present(B) on the navigation manager (or the within FragmentA). When you want to add C to the stack but remove B, you can call clearNavigationStackToRoot. This will clear the current navigation stack to the first fragment that you added to the manager (aka FragmentA) without any navigation. Then present(C). This will cause no animation to occur for the clearNavigationStackToRoot, but will show an animation for the present(C).
If you need more clarification I can add a sample to my repository for you. The more samples the more resources people will have to utilize my library!
I have added an example of what you need under the DrawerClearStackExample package in the sample application for the library.
Thanks,
DMCApps

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 FragmentManager: Custom animation shows wrong exit fragment

I'm using the SupportFragmentManager to navigate between different fragments (let's call them A, B, C, D for simplicity). The navigation itself works correctly.
The animation works only correctly considering the entering fragments. My code is basically the same for navigating between all fragments:
activity?.supportFragmentManager?.commit {
setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right)
replace(R.id.myNavHostFragment, BFragment())
}
The navigation is only linear, so I navigate from A -> B, B -> C, and C -> D.
The problem is: only the animation of A -> B works correctly.
For all others, during the animation, the wrong 'old' (exit) fragment A is shown in background, while the new one slides in.
So this means:
animation A -> B works fine.
animation B -> C does not work correctly. Fragment C slides in correctly. But while the animation plays, not Fragment B is shown in the background. Instead, somehow Fragment A is shown again.
the same for C -> D. Fragment A is shown, while C should be seen during the animation.
As you can see, I do not add fragments to the backstack. I really can't figure out why Fragment A is always shown during the animation, even if it should be a different one.
In a nutshell: the animation shows always the first fragment (A) as exit fragment, while it should be the previous one instead.
Please check your navigation graph and re-visit all the below attributes shown in below image.
Open your own navigation_graph.xml and then click on Design tab.
You will see below attribute(as shown in image) when you click on navigation direction.
Also, Animation has nothing to do with the back stack of your app.
You need to check Pop Behavior and Launch Options
Also please see if you have set any Flag's incorrectly in your code.
For e.g. FLAG like FLAG_ACTIVITY_CLEAR_TOP
And finally please thoroughly go through this Article

Go to a specific fragment from anywhere keeping backstack

I'm updating the old navigation system from my app to jetpack navigation and I'm facing an issue:
From a DialogFragment which can be displayed from almost any fragment in the app, I have a button which leads to a specific Fragment. I've found in the documentation global actions, but it seems they clear the entire navigation backstack when you navigate with one of these and I need to keep my backstack, just add this new fragment to it from anywhere. What's the solution to this problem ?
Thanks

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.

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.

Categories

Resources