
First, the code:
@Composable fun MainScreen( modifier: Modifier = Modifier, viewModel: MainViewModel = androidx.lifecycle.viewmodel.compose.viewModel() ) { val state = viewModel.state.collectAsStateWithLifecycle() val navController = rememberNavController() Scaffold( bottomBar = { NavigationBar { listOfNavigationDestinations.forEach { NavigationBarItem( selected = it == state.value.currentDestination, onClick = { if (it != state.value.currentDestination) { viewModel.updateCurrentDestination(it) navController.navigate(it.route) { popUpTo(NavigationDestinations.Reference.route) { inclusive = false } } } }, icon = { Icon( painter = painterResource(id = it.icon), contentDescription = it.title, modifier = modifier.size(28.dp) ) }, label = { Text( text = it.title, style = microTextStyle.copy( fontSize = 14.sp ) ) } ) } } } ) {innerPadding -> Box( modifier = modifier.padding(innerPadding) ) { NavigationGraph( navHostController = navController ) } } } What is the issue?
After navigating from one screen to another - and after that when I press back button - the screen navigates to NavigationDestinations.Reference as expected. BUT, the icon in the NavigationBar is still highlighted for the Navigation.Interpolation screen - meaning it doen't change with back button press.
How do I address that?
I don't understand how to handle Backpress event and sync that with the selection procedure.
Источник: https://stackoverflow.com/questions/781 ... the-select
Мобильная версия