'Saving State with Bottom Navigation View
I'm currently writing an app that displays a list of movies. I'm using a bottom navigation view with 3 tabs: Trending, Discover, and Favorites. Each of these display a list of movies and the user can press on the movie to go to the details fragment in each one, also I'm using one nav graph.
When I scroll a bit and go to the details and use the back button, the state of the recyclerview is saved. However if I go to another tab and come back the state is not saved. After extensive research I still can't figure out the answer.
Any help is appreciated.
Solution 1:[1]
I dare to assume that you do have not a dependency on the navigation fragment:
kotlin: androidx.navigation:navigation-fragment-ktx:$nav_version
java: androidx.navigation:navigation-fragment:$nav_version
Solution 2:[2]
Problem caused by Navigation Component. It use replace (Fragment) instead of add (Fragment). You can remove Navigation Component and manage Fragment by yourself, use add instead of replace fragment.
Solution 3:[3]
Found the answer: The bottom navigation now automatically handles the save state, just make sure you have the latest versions of nav and fragment. You can find the latest nav version and fragment versions here:
https://developer.android.com/jetpack/androidx/releases/navigation
https://developer.android.com/jetpack/androidx/releases/fragment
Here is an article that explains everything you need to know
https://medium.com/androiddevelopers/navigation-multiple-back-stacks-6c67ba41952f
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Eugene_Sh |
Solution 2 | Nam Anh |
Solution 3 |