star_zero
05/01/2022, 2:23 AMNavHost(navController = navController, startDestination = "tab1") {
navigation(startDestination = "screen_a", route = "tab1") {
composable("screen_a") { /* */ }
composable("screen_b") { /* */ }
}
navigation(startDestination = "screen_c", route = "tab2") {
composable("screen_c") { /* */ }
composable("screen_d") { /* */ }
}
composable("screen_common") { /* */ }
}
BottomNavigationItem is implemented as follows. Same as the document.
BottomNavigationItem(
selected = currentDestination?.hierarchy?.any { it.route == tab.route } == true,
// ...
)
Ian Lake
05/01/2022, 3:44 AMstar_zero
05/01/2022, 4:18 AMBottomNavigation {
var selectedIndex by rememberSaveable { mutableStateOf(0) }
tabs.forEachIndexed { index, tab ->
BottomNavigationItem(
selected = selectedIndex == index,
onClick = {
if (selectedIndex == index) {
(navController.findDestination(tab.route) as? NavGraph)?.let {
navController.popBackStack(it.startDestinationId, false)
}
} else {
navController.navigate(tab.route) {
popUpTo(navController.graph.id) {
saveState = true
}
launchSingleTop = true
restoreState = true
}
selectedIndex = index
}
},
icon = {
Icon(imageVector = tab.icon, contentDescription = null)
},
label = {
Text(text = tab.label)
},
)
}
}