Android Jetpack Navigation proper back stack with BottomNavigationView - android

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

Related

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.
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

Fragments are always recreated when used with Android navigation from jetpack

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

Android menu option problems in fragments

I am developing an application which has a navigation drawer with 9 fragments.
Two of nine fragments have tabs(4) interface implemented. Each tab has its own menu option and each fragments (other than with tabs) have their own menu options.
Now the issue is,
When i access or switch a simple fragment(without tabs) from the navigation drawer then its menu options and working fine.
BUT
When switching from a tabbed fragment after accessing , its menu options now appear on every other fragment in the navigation menu.
For example
I access and switch from fragment "A" ->everything goody including all menu options
I access and switch from fragment "B" (with tabs) then all the other fragments have its menu options.
If I didn't explain my problem effectively then do ask me again in the comments. I have been stuck with this problem since a week. Maybe a small problem or something else but do need some help from the people here.
////update//
SetHasMenuOptions implemented in each fragment. All fragments working fine except the tabbed ones
The main problem is, after accessing tabbed fragments all the menu options of other fragments are replaced by the tabbed menu options.
3 posting a question through my phone cant post code sample.
Actually first i had problems like this with every fragment but then i implemented sethasmenuoptions
And menu.clear() in each fragment and every fragment seems to work
Like it should except the tabbed ones
Hope this info helps
Thanks
Use transaction.addToBackStack(null) while fragment transaction and in layout set background colour to the fragments .

Need clarification on how the native Navigation Drawer works with Fragments

I'm used to using a 3rd party library for implementing a navigation drawer (sliding menu) in Android, but I want to use the native version from now. I went through the tutorial (http://developer.android.com/training/implementing-navigation/nav-drawer.html) which is pretty straight forward.
I typically extend the 3rd party navigation drawer to each activity by defining the configuration in a base class. The new nav drawer, however, swaps fragments in and out, which my research indicates is the standard way of managing your displays.
This seems fine, but my app has a fairly complex hierarchy of pages and navigation. Like most apps, it contains more fragments than just the ones in the menu.
So if I have 3 nav drawer items for fragments A, B, and C, and I can only load fragment D from fragment C, do I handle that navigation logic in the activity where I configure the nav drawer? It seems like kind of a nightmare to have one container to swap out an indefinite number of fragments, especially if the work flow is deep.
From what I can gather on Stack, there seem to be a lot of people who are familiar with extending a 3rd party drawer in each Activity, but when they switch over to the native version there is confusion.
So to summarize, I understand the fragment swapping aspect of the navigation. I just don't understand how the rest of the work flow navigation would work, say if I had several detail screens below a nav item fragment. If anyone can give me some hints on how best to approach this scenario, maybe I can experiment and post some code for future readers.
For navigation in Android there is always one thing you have to remember:
If you stay on the same level of the navigation hierarchy, for
example when swiping through pages, you use Fragments.
When you move up and down in the navigation hierarchy, for example
going to a detail view, you would start a new Activity and displayed the
Fragment with the detail content in it.
A NavigationDrawer is used for top level navigation in your app to quickly navigate between different parts of your app. It's kind of like a main menu. With that in mind you need to determine if a NavigationDrawer makes sense in your app. It's all about how the user should navigate through the content. If there is just one path for the user to follow for example if you start out with just one screen and from then on the user can just go deeper and deeper in the navigation hierarchy from one detail view to the next than a NavigationDrawer does not make much sense. But if there are multiple paths the user can take that lead into different, independent parts of your app without one dedicated start screen on which everything else depends than a NavigationDrawer sounds pretty reasonable.
You can look at Google apps like Gmail, Drive or Google+ to see how a NavigationDrawer is supposed to be used.

Android Navigation Drawer Design

I have an existing application that has about 25 activities that are navigated to from a "dashboard". I would like to switch and start using the Navigation drawer and fragments. I have gone thru the Nav Drawer design pages online and the example app. My question is what is the best way to convert (structure) my app to fit the Nav Drawer pattern. If I switch my activities to be fragments and use a main activity to replace each fragment as navigation happens, but not sure if that is good b/c for a tablet layout, I might want multiple fragments on my view and not sure if this will limit me. If I go with the other direction I was thinking, keep all my activities and just switch the necessary ones to fragments for tablets but I would need each activity to create the navigation drawer (I think ?) which in my case the drawer is dynamic based on server data. Any suggestions would be great.
Thanks
Brandon
Navigation drawer has to be created for each activity, although you could inherit Activity and create a parent class that handles navigation drawer specific code if code duplication is a concern.
Using a drawer does not limit you to use one fragment per screen, just listen to onClick in drawer and initiate as many fragment transactions as you need.
When it comes to structuring your app, there is no universal advice, I would recommend you to watch Google I/O 2013 talk - Structure in Android App Design. Navigation Drawer is kind of the main theme of the talk.

Categories

Resources