BottonNavigationView Icon not changing on back press - android

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)

Related

Hiding a BottomNavigationView during a fragment transition

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.

Issue with Back stack flow using Navigation component and BottomNavigationView

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

BottomNavigationView in child fragment in Navigation Component

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.

Navigate with BottomNavigationView inside fragment (not activity) using navigation component and navigation graph

I have a single Activity application. I created a single Navigation graph containing all the possible destinations (Fragment).
The Activity layout only contains a <fragment/> container as it should be.
There is a main Fragment containing a BottomNavigationView where the user can navigate to all the important destinations (3 of them).
When I implement the BottomNavigationView as described in many tutorials or event in the official documentation, it replaces the full content of the Activity. Therefore, the BottomNavigationView is no more displayed as it is contained in a Fragment and not in the Activity. I want it to inflate the right layout in the main Fragment.
Should I use another Navigation graph, specific for the main Fragment ?
Should I use a classic Listener (But I will loose all the interest of using a Navigation graph) ?
Is there a solution I don't know ?
How do I implement it ? I tried to create two different Navigation graph, but I can not set it to my BottomNavigationView
Answer:
Create a second Navigation graph containing the destination you want your BottomNavigationView to access.
In the fragment containing the BottomNavigationView make sure to insert a FragmentContainerView and an appropriate ID.
Then, thanks to This answer right here you can implement it easily.
Juste refer to the nested NavHost using this:
val nestedNavHostFragment = childFragmentManager.findFragmentById(R.id.bottom_nav_host) as? NavHostFragment
val navController = nestedNavHostFragment?.navController
val bottomNavigationView = view.findViewById<BottomNavigationView>(R.id.bottom_navigation)
if (navController != null) {
bottomNavigationView.setupWithNavController(navController)
} else {
throw RuntimeException("Controller not found")
}

how to call navigation.navigate inside recyclerView item

I have a recyclerView inside a fragment, and when I click an item I want to navigate to next fragment. At the first time I click, it's OK, but after getting back to this fragment and clicking the same item again my application crashes. Could anyone please tell me how to call navigation.navigate inside recyclerView item?
If you are using the navigation compenent, and inside the recyclerview, you need to go to another class, use the following:
Navigation.findNavController(itemView).navigate(
UIFragmentVideoListDirections.aVideoList2Video((aFile.absolutePath))
)
Please try and let me know.
Added Answer:
NavHostFragment — implementation of NavHost for creating fragment destinations. It has its own NavController and navigation graph. Typically you would have one NavHostFragment per activity.
Navigation — helper class for obtaining NavController instance and for connecting navigation to UI events like a button click.
NavigationUI — class for connecting app navigation patterns like drawer, bottom navigation or actionbar with NavController.

Categories

Resources