Home > Enterprise >  Navigation resets when switching fragments
Navigation resets when switching fragments

Time:10-30

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?

it no work

CodePudding user response:

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.

  • Related