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.
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
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.
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
Related
I am facing this issue of fragments recreation with Android navigation component. I have integrated bottom navigation and coupled it with the android navigation component. So, every time I click on a tab on bottom bar, the fragment is recreated and the old state is not persisted.
I want to retain the state of fragment even when I go to other tabs and come back. I am not finding any solution for it anywhere.
Except bottom navigation, I am using navController.navigate() method to navigate between different fragments.
Hi the problem is fixed in latest version 2.4.0-alpha01 now there is a support multiple backstack navigation
Checkout the link:
https://developer.android.com/jetpack/androidx/releases/navigation#version_240_2
There is a problem with Navigation to handle multiple back stacks. A similar solution to this problem is available here
You can also use ViewModels to preserve the view state.
As alternative you can use hide/show fragment instead of navigation:
Show hide fragment in android
I am using new Navigation component of Jetpack Library and it is pretty cool but i experience an annoying problem.
I use Navigation component with Bottom navigation view and i have three fragments: Home, Profile and Settings. Home fragment has RecyclerView and when user clicks an item from it, a new fragment appears. The problem is when i am in some state in Home fragment and go to for example profile fragment, this state is gone and new home fragment is created and i do not want this to happen.
How can i solve this problem?
Thanks in advance.
I think this issue exists in current navigation. In this year's dev summit they talked about fixing that issue.
I have a problem I am using the new navigation controller from Android Jetpack. I have 3 items in my bottom navigation menu, and the first item has 5 fragments one after another. When I click to the second item in bottom navigation, and return to my first item it opens the first fragment not the last opened. Because android:id="#+id/destination_home" in Menu is the same as the same as id in my navigation graph.
Can anyone advice the best solution? Thanks
For now navigation component not supports of saving "multi stack". This issue can be fixed with custom navigation controller: https://github.com/STAR-ZERO/navigation-keep-fragment-sample
P.S.: also in this thread guy has same problem and you can see the answer, maybe it will help you.
OBS: Though still no first class support (as of writing this), Google has now updated their samples with an example of how they think this should be solved: https://github.com/googlesamples/android-architecture-components/tree/master/NavigationAdvancedSample
The Android Codelab for Navigation does a good job describing how to use the architecture component Navigation together with a BottomNavigationView. But let's say I have 2 tabs in the BottomNavigationView, Tab1 and Tab2. And let's say that in Tab1 you navigate through the fragments Frag1 --> Frag2. Now, whenever I go to Tab2, and then back to Tab1, the fragment back stack of Frag1,2 is gone, and replaced with the starting point Frag1 again.
What do I have to do in an app so that a BottomNavigationView together with Navigation keeps its back stack intact even though I change tabs? And, also keeping the back/up button behaviours in sync with the guidelines.
Previously I've done this with the use of ViewPager and managing the back stack my selfe, but that doesn't feels like the right approach with the new Navigation.
Thanks in advance!
Edit:
There's a more elaborate answer here.
The major reason is you only use one NavHostFragment to hold the whole back stack of the app.
So the solution is each tab should hold its own back stack.
In your main layout, wrap each tab fragment with a FrameLayout.
Each tab fragment is a NavHostFragment and contains its own navigation graph in order to make each tab fragment having its own back stack.
Add a BottomNavigationView.OnNavigationItemSelectedListener to BottomNavigtionView to handle the visibility of each FrameLayout.
If you don't want to keep all the fragments in memory, you can use app:popUpTo and app:popUpToInclusive="true" to pop out the ones you don't want to keep.
This is currently not supported in the new Navigation Architecture. I was also pretty bummed by this, as it is a very basic feature in today's apps, and a lot of the apps are now using the bottom navigation. There is a running thread, if you wanna keep an eye on it. They are saying they will come up with a long term solution for this, but for the shorter run, they are gonna give a sample on how to tackle this. https://issuetracker.google.com/issues/80029773#comment25
I develop one android app with navigation drawer.in that navigation drawer using fragment for each item pages. I want to get data from one fragment to another fragment in navigation drawer. How to solve this task?
It does not matter if it is in Navigation Drawer. Use the same method to pass data between fragments as in developer docs
https://developer.android.com/training/basics/fragments/communicating.html
other link as mentioned in comments seems good too
https://www.google.co.in/amp/s/www.journaldev.com/14207/android-passing-data-between-fragments/amp