I have a code which calculates if bottom navigation bar should be visible, but once I got to specific screen the navigation resets to first home tab. I guess recomoposition occurs for whole Scaffold, but can’t see why. I’m using new type safe compose navigation if that matters.
Here is a relevant code:
@Composable
fun HomeNav() {
val navController = rememberNavController()
var shouldShowBottomBar by rememberSaveable {
mutableStateOf(true)
}
var selectedItemIndex by rememberSaveable {
mutableIntStateOf(0)
}
LaunchedEffect(key1 = navController) {
navController.addOnDestinationChangedListener { _, destination, _ ->
shouldShowBottomBar = !destination.hasRoute(Details::class)
if (destination.hasRoute(Home::class)) {
selectedItemIndex = 0
}
}
}
Scaffold(
bottomBar = {
AnimatedVisibility(visible = shouldShowBottomBar) {
NavigationBar {
bottomNavItems.forEachIndexed { index, item ->
NavigationBarItem(
selected = selectedItemIndex == index,
onClick = {
selectedItemIndex = index
navController.navigate(item.destination) {
popUpTo(Home) {
saveState = true
}
launchSingleTop = true
restoreState = true
}
},
...
)
}
}
}
}
) { innerPadding ->
NavHost(
modifier = Modifier.padding(innerPadding),
navController = navController,
startDestination = HomeGraph
) {
homeNavGraph(navController)
}
}
}