I'm using Android Navigation Component. There is one activity with FragmentContainerView(NavHostFragment) with simple navigation flow by common_navigation_graph
But on one fragment there is a nested NavHostFragment and BottomNavigationView. Reason for that - I want to have 3 tabs on the fragment. Bottom navigation configure with own navController and separate navigation_graph
val navController = activity?.findNavController(R.id.navHostViewPagerView)
binding?.bottomNavigation?.setupWithNavController(navController!!)
On each Tab there is a one more nested fragment with own FragmentContainerView and it's own small tab_navigation_graph
Navigation scheme looks:
[enter image description here][1]
[1]: https://i.stack.imgur.com/gmXOY.png
In general it works pretty well. Navigation between tabs, navigation in some selected Tab works well, back stack works well
override fun handleOnBackPressed() {
view?.findNavController()?.popBackStack()
}
But since there was a navigation to some next fragment from common_navigation_graph(SomeNextFragment on the picture) the backStack flow will be broken.
I will be able to return to fragment with Tabs, but backStack flow for nested fragments in the Tab will not work. Only the last fragment will be displayed(Tab1.2) Transition by Blue arrow not working.
On handleOnBackPressed() view==null and fragment.id==0. Fragment was destroyed
Question is how to save ability to navigate through nested Tab fragments on come back from other fragmnet from common_navigation_graph
Each tab should hold its own back stack.
https://github.com/googlesamples/android-architecture-components/tree/master/NavigationAdvancedSample
Related
I am facing a challenge in the app that i am developing.
I have a BottomNavigationView with 4 Fragments:
Home Feed Notification Profile
Home is the home fragment of my NAV GRAPH.
Lets suppose Home has a button and on click of it, it goes to CartFragment. Now i click to Feed by using BottomNavigationView. So now, when i click on home fragment again from BottomNavigation view...i see the CartFragment whereas i want to see the HomeFragment. And it doesn't even show the HomeFragment tab active on the bottomNavigationView, it is like it completely replaces it.
I am not able to switch to my original fragment whenever i click on a tab from BottomNavigationView
If you want to reset your backstack when you press one of the bottom nav items, use below code where you define bottom nav view and this will solve your problem:
yourBottomNavView.setOnItemSelectedListener { item ->
NavigationUI.onNavDestinationSelected(item, yourNavController)
yourNavController.popBackStack(item.itemId, inclusive = false)
true
}
Just change yourBottomNavView and yourNavController with yours.
I have a hard time with trying to learn Navigation Components.
In my app, I have 6 fragments.
Four of them are swipeable by using ViewPager 2
Fifth fragment is not implemented in ViewPager, so user can't go there by swipping
Sixth fragment is just a host for ViewPager, since the other fragments are used by Navigation Components
I can swipe fragments for now, but whenever I try to use Navigation Components, I getting error about action/destionation. It's simply about calling for example ActionFrag1ToFrag5 from my Host Fragment
It seems like I can browse through fragments, but I can't cast any Navigation Component functions, because the fragment stays the same, it doesn't change label after swipe to another one.
Is there a way to get proper NavController from specific fragment, or it's just my poor implementation?
If Frag1, Frag2, Frag3, and Frag4 are those in the ViewPager and you want to go to Frag5.
The direction needs to be from the Frag6 that is hosting the ViewPager to the Frag5.
I am using navigation component for bottom navigation component.
bottomNavbar.setupWithNavController(navController)
now this is working fine but when I hit the back button, it is returning to the home page but the icon is not changing, it is stuck in the previous selected fragment. I have three fragments and I have implemented navbar separately in all those fragments, here's the code for those three fragments.
settings fragment
val bottomNavbar = view.findViewById<BottomNavigationView>(R.id.bottomNavbar)
bottomNavbar.setupWithNavController(navController)
search fragment
val bottomNavbar = view.findViewById<BottomNavigationView>(R.id.bottomNavbarSearch)
bottomNavbar.setupWithNavController(navController)
chat fragment
val bottomNavbar = view.findViewById<BottomNavigationView>(R.id.bottomNavbar)
bottomNavbar.setupWithNavController(navController)
here search fragment is my home fragment.
Is there a mistake in my implementation or should I just switch to the old way of implementing bottom navigation view.
any help is appreciated. Thanks
Make sure to have the same ids in the bottomNavView menu that matches
the corresponding ones in the navGraph of the bottomNavView fragments.
check this answer
It seems you have added BottomNavigationView on all three fragments which should ideally be on Activity's view below your fragment/FragmentContainerView where you have defined your navGraph.
To fix it you need to remove BottomNavigationView from all 3 fragments and add it on Activity using the following basic structure.
Activity XML structure:
<constriantLayout>
<fragment/> //with navGraph
<bottomNavigationView/>
</constraintLayout>
and onCreate of Activity use the code setting up BottomNavigationView
val bottomNavbar = findViewById<BottomNavigationView>(R.id.bottomNavbar)
bottomNavbar.setupWithNavController(navController)
I am creating an app using single Activity so on main_activity I set the fragment as NavHostFragment and on the bottom the BottomNavigationView.
In fragment A (first tab selected), If I click on button_1...navigate on a new fragment (A1). Of course inside fragment A1 I need to hide BottomNavigationView so on main_activity I am using addOnDestinationChangedListener to hide BottomNavigationView.
The problem is that if I want use a transition (enter, exit, popup_enter, popup_exit) the BottomNavigationView hide immediatly. (example 1 on image)
How can I do to have the same as example 2 on image?
It looks like you have your 'NavHostFragment' below the 'BottomNavigationView'. So even if you were to delay hiding the BottomNavigationView the fragment transition would still happen behind it. So without changing your implementation completely it won't be possible to have the new fragment transition above the bottom BottomNavigationView.
I want to achieve something like this image flow in android navigation component. Where the Dashboard Fragment is the start destination. And from here i can navigate to another fragment which have a bottom navigation view. Is this possible using a single nav graph and a single activity? What is the best way to achieve something like this?
You can use BottomNavigationView
val childNavView: BottomNavigationView = binding.nav1View
val navController = NavHostFragment.findNavController(childFragmentManager.findFragmentById(R.id.navHostFragment) as NavHostFragment)
childNavView.setupWithNavController(navController)
In case someone's still looking for a solution.
Just use two separate navigation graphs. And when navigating, pick the right navigation controller object. You can use a single activity of course, with nested fragments. So your "Bottom nav page" will have its own navigation container with child fragments in it.
Moreover, you can also navigate from the inner nodes to outer ones. In that case you need to reference the parent fragment ("Bottom nav page") from the child fragment and then obtain its navigation controller like this:
parentFragment?.parentFragment?.findNavController()
Here the first parentFragment is NavHostFragment and its parent is your actual parent fragment ("Bottom nav page").
If you want Page 1-3 to have options for further fragments to navigate inside (i.e. Page 1.1, 1.2 for Page 1, etc), then you need a navgraph for each Page (so 3 total), I would also make them all the root of their own navigation, would make it a lot easier to implement, and just replace the dashboard fragment with an activity if it's for signing in for example.