Architecture-components-samples: [NavigationAdvancedSample] DestinationChangedListener receive events just for the first graph

Created on 20 Oct 2020  路  1Comment  路  Source: android/architecture-components-samples

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.

Most helpful comment

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.

>All comments

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.

Was this page helpful?
0 / 5 - 0 ratings