I have created a small sample app to test out the android navigation library. The general idea is that I want two tabs with their own navigation graph.
My main activity layout contains a BottomNavigationView with two NavHosts.
The ButtonNavigationView click listener takes care of showing one NavHost or the other and also calling NavigationUI.setupActionBarWithNavController with the selected controller to update the toolbar accordingly. Up navigation is working fine.
The problem I am facing right now is the back button.
There is a property app:defaultNavHost="true" which ensures that your NavHostFragment intercepts the system back button, but I want that to be on/off depending on the active graph. I couldn't find a way to change it :(
I know I can override onBackPressed() but I am trying to find a way for the library to do that work for me letting it know which graph is active.
As per the NavHostFragment source, all that app:defaultNavHost="true" does is call through to setPrimaryNavigationFragment(), which is how FragmentManager knows which child Fragment to send back button events to.
Therefore, when switching to a new Fragment, you can add setPrimaryNavigationFragment() to your FragmentTransaction to get the same behavior.
Related
I have the following navigation xml on a BottomNavigationView, however when the user navigates inside the 1st fragment and then navigates to another fragment using the BottomNavigationView and then comes back to the initial fragment, it resets all the way to the default 1st fragment the navigation to a deeper fragment does not persist, i assume this is because the fragment gets recreated and from what i've looked at on other questions is that there is a way to save the data to re-set any text values on text fields or similar stuff, however i haven't seen if theres a way to avoid the fragment from being destroyed and re-created in order to make the navigation and whatever changes the user had previously made to the view persist.
Is there a way to do that, or should i use a savedInstance in such a way that it re-navigates to the previously navigated fragment?
Part of Navigation 2.4.0 is support for multiple back stacks - i.e., the ability for each tab of a bottom navigation bar to save its state, restoring that state when you reselect that tab.
As per that blog post:
If you’re using NavigationUI, our set of opinionated helpers for connecting your NavController to Material view components, you’ll find that multiple back stacks is enabled by default for menu items, BottomNavigationView (and now NavigationRailView!), and NavigationView. This means that the common combination of using navigation-fragment and navigation-ui will just work.
This means that if you are using the setupWithNavController methods, then upgrading to Navigation 2.4 will give you support for multiple back stacks immediately. You can verify that by going to your order fragment (thus building a back stack on that first tab), going to a different tab, then tap on the first tab again to reselect it.
Of course, it is your fragments state, not the instances themselves that are saved and restored. This means that each individual fragment must still save its state properly.
This question is related specifically to the androidx.navigation library.
I split up my primary graph into 2 graphs because I wanted to have one with a bottom nav with the fragments above it and one without. Instead of using <include... I added the activity to the first graph
<activity
android:id="#+id/Activity2"
android:name="com...Activity2"
android:label="Activity2" />
This all works nicely, but I also have a splash screen in the first graph that checks if the user is authenticated and navigates them directly into the second graph. With a fragment I can just use the standard popTo and popToInclusive to manage the fragment backstack but I have not been able to figure out how to do this with two activities so that when the second activity is launched the first is killed and removed from the backstack so the user cannot navigate backwards.
Currently I am just handling it in the fragment where the navigation occurs
navController
.navigate(R.id.action_someFragment_to_anotherFragment)
requireActivity().finish()
and this works but it leaves room for error and I'd like to deal with it with the navigation library if possible.
Each individual NavController is totally independent from one another. While an <activity> destination allows you to use navigate() to go to an entirely separate activity (which may or may not use Navigation itself), Navigation itself will never finish() an activity as part of a navigate() call so you'd need to do that yourself.
Using multiple activities with different navigation graphs is not the recommended way to handle authentication in Navigation as per the the Navigating Navigation talk and this approach fails in many ways (such as deep linking and invalidation after process death/recreation) that are correctly handled by the guide for handling login. When using one NavController and the ability to listen for navigation events, you do not run into these issues.
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
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.
I'm trying to implement this solution using Google Jetpack Navigation component and still struggle with back stack issue.
The problem I'm facing is a custom implementation of navigating method in FragmentNavigator class breaks existing logic of back stack.
if I try to add simple behavior like:
transaction.addToBackStack(tag)
then BottomNavigationView is not highlighting/selecting buttons properly for a current active fragment when the back button is pressed.
My question how to implement properly custom back stack using such approach that works correctly with BottomNavigationView and properly using the back pressed listener to override more advanced custom back stack that put right navigation in the same BottomNavigationView?
The naive approach would be override onBackPressed in activity and then just create own logic separately and manually use navController.navigate to simulate back stack functionality, but I would like to hear how this problem can be solved with BottomNavigationView, NavController, NavHostFragment, FragmentNavigator scope.