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.
Related
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
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 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 have a problem...I am developing an app in android studio.In the meantime,there is already a navigation drawer in it.But the problem is, it only appears in the home screen.When it goes to another screen, the navigation drawer is not shown. I am new to android studio but i know some of Java languages.Can anyone help me with providing anything that would help me make the navigation drawer show in all of slides and pages in my app.
from docs
Navigation Drawer
The navigation drawer is a panel that displays the app’s main
navigation options on the left edge of the screen. It is hidden most
of the time, but is revealed when the user swipes a finger from the
left edge of the screen or, while at the top level of the app, the
user touches the app icon in the action bar.
for achieve what you asked you have to use Fragments
Fragment represents a behavior or a portion of user interface in an
Activity. You can combine multiple fragments in a single activity to
build a multi-pane UI and reuse a fragment in multiple activities. You
can think of a fragment as a modular section of an activity, which has
its own lifecycle, receives its own input events, and which you can
add or remove while the activity is running (sort of like a "sub
activity" that you can reuse in different activities).
see this examples for more details
1. Navigation Drawer - android hive
2. Navigation Drawer exp 2
3. Navigation Drawer exp 3
Refer this answer also
Answer
You have to add fragments in that activity where the navigation drawer exist. Whenver the user will click on the option in the navigation drawer the view should be changed by replacing with the required fragment.So by using the fragments user will stay on the same activity but just the views will be changed in that activity. you can refer to the fragments documentation provided by android developers. https://developer.android.com/guide/components/fragments.html