highlight parent nav Icon

Viewed 34

I have an app that has a bottom navigation bar with 3 icons. Each one of those icons maps to their own screens and each one of those screens can navigate to n amount of screens only available through it. How can I highlight the parent of the current screen on the navigation bar? I am using jetpack navigation components and xml and it seems like everything is being done automatically so I have no way of dictating behavior by hand.

1 Answers

Consider using NavController.OnDestinationChangedListener

For example:

private fun NavDestination.matchDestination(@IdRes destId: Int): Boolean =
    hierarchy.any { it.id == destId }

private val onDestinationChangedListener = NavController.OnDestinationChangedListener { _, destination, _ ->
    viewBinding.bottomNavigationView.menu.forEach { item ->
        if (destination.matchDestination(item.itemId)) {
            item.isChecked = true
        }
    }
}

navController.addOnDestinationChangedListener(onDestinationChangedListener)
Related