navController 不更新 Jetpack Compose 中的位置
navController not updating locations in Jetpack Compose
我已经为我的新闻应用构建了 NavigationRail,它可以让我导航到搜索屏幕和主屏幕。我遇到一个问题,我传递给我的 NavigationRail 的 NavController 没有给我更新的当前位置,因此我的 navigationRail 图标没有显示它们在目的地时被选中。
我的导航栏代码是:
@Composable
fun NewsNavigationRail(
navController: NavHostController
){
val navigationItems = Destinations.values()
NavigationRail{
navigationItems.filter{
it.icon != null && it.label != null
}.forEach {
NavigationRailItem(
icon = { Icon(
it.icon!!,
contentDescription = it.name
) },
// problematic line --------------------------
selected = navController.currentDestination?.route == it.route,
onClick = {
},
label = { Text(it.label!!) }
)
}
}
}
我的 NavigationHost 代码是:
@OptIn(ExperimentalMaterialApi::class)
@Composable
fun NavigationHost(){
val navController = rememberNavController()
Row {
NewsNavigationRail(
navController = navController
)
NavHost(navController = navController, startDestination = Destinations.HomeScreen.route){
composable(Destinations.HomeScreen.route){
val newsViewModel = hiltViewModel<NewsViewModel>()
HomeScreen(newsViewModel, navController)
}
composable(
"${Destinations.NewsScreen.route}/{url}",
arguments = listOf(navArgument("url"){
type = NavType.StringType
})
){
val url = it.arguments?.getString("url")
NewsScreen(url)
}
composable(Destinations.SearchScreen.route){
val newsViewModel = hiltViewModel<NewsViewModel>()
SearchScreen()
}
}
}
}
试试这个,
val navBackStackEntry by navController.currentBackStackEntryAsState()
val currentRoute = navBackStackEntry?.destination?.route
我已经为我的新闻应用构建了 NavigationRail,它可以让我导航到搜索屏幕和主屏幕。我遇到一个问题,我传递给我的 NavigationRail 的 NavController 没有给我更新的当前位置,因此我的 navigationRail 图标没有显示它们在目的地时被选中。
我的导航栏代码是:
@Composable
fun NewsNavigationRail(
navController: NavHostController
){
val navigationItems = Destinations.values()
NavigationRail{
navigationItems.filter{
it.icon != null && it.label != null
}.forEach {
NavigationRailItem(
icon = { Icon(
it.icon!!,
contentDescription = it.name
) },
// problematic line --------------------------
selected = navController.currentDestination?.route == it.route,
onClick = {
},
label = { Text(it.label!!) }
)
}
}
}
我的 NavigationHost 代码是:
@OptIn(ExperimentalMaterialApi::class)
@Composable
fun NavigationHost(){
val navController = rememberNavController()
Row {
NewsNavigationRail(
navController = navController
)
NavHost(navController = navController, startDestination = Destinations.HomeScreen.route){
composable(Destinations.HomeScreen.route){
val newsViewModel = hiltViewModel<NewsViewModel>()
HomeScreen(newsViewModel, navController)
}
composable(
"${Destinations.NewsScreen.route}/{url}",
arguments = listOf(navArgument("url"){
type = NavType.StringType
})
){
val url = it.arguments?.getString("url")
NewsScreen(url)
}
composable(Destinations.SearchScreen.route){
val newsViewModel = hiltViewModel<NewsViewModel>()
SearchScreen()
}
}
}
}
试试这个,
val navBackStackEntry by navController.currentBackStackEntryAsState()
val currentRoute = navBackStackEntry?.destination?.route