I'm using NavigationAdvancedSample in order to properly implement the navigation component with the bottom bar.
For each tab in the bottom bar, I've created a separate navigation graph (3 graphs in total).
In the MainActivity, on the NavController instance, I've added OnDestinationChangedListener in order to control BottomBar visibility for specific fragments.
But the problem is that the OnDestinationChangedListener receives event's just for the first (home) tab/graph, but not for the second two.
controller.value?.addOnDestinationChangedListener { _, destination, _ ->
when (destination.id) {
R.id.frag1-> bottomNavigationView.visibility = View.GONE
else -> bottomNavigationView.visibility = View.VISIBLE
}
}
Is it possible to achieve the desired behavior, as described above, using this sample?
Thank you.
Looks to me like you're only settings the OnDestinationChangedListener once for the first controller and not the rest, where exactly in your Activity are you setting it? I'd recommend setting it inside the controller observer like line 68 of MainActivity.
controller.observe(this, Observer { navController ->
setupActionBarWithNavController(navController)
navController.addOnDestinationChangedListener { _, destination, _ -> ... }
})
You probably want to save the listener and current controller in a variable and remove the listener every time the current controller changes too.
Most helpful comment
Looks to me like you're only settings the
OnDestinationChangedListeneronce for the first controller and not the rest, where exactly in yourActivityare you setting it? I'd recommend setting it inside thecontrollerobserver like line 68 of MainActivity.You probably want to save the listener and current controller in a variable and remove the listener every time the current controller changes too.