I need an implementation that follows this navigation pattern. For example, there are screens a, b, c, and d. The navigation sequence is a > b > d > c > b > d. When we are on screen d and press the back button, the navigation should go as follows: d > b > c > a > exit.
I tried to implement it like this, but it works incorrectly.
@Composable
fun BottomNavBar(navController: NavController, currentDestination: NavDestination?) {
NavigationBar(modifier = Modifier.height(120.dp)) {
topLevelRoutes.forEach { topLevelRoute ->
NavigationBarItem(
selected = currentDestination?.hierarchy?.any { it.hasRoute(topLevelRoute.route::class) } == true,
onClick = {
if (!navController.popBackStack(topLevelRoute.route, false)) {
navController.navigate(topLevelRoute.route) {
// Avoid multiple copies of the same destination when
// reselecting the same item
launchSingleTop = true
// Restore state when reselecting a previously selected item
restoreState = true
}
}
},
icon = { Icon(topLevelRoute.icon, contentDescription = null) },
label = { Text(topLevelRoute.name) })
}
}
}