Jetpack Compose + 导航:从不同的 BottomBar 屏幕导航到 BottomBar 目的地
Jetpack Compose + Navigation: navigating to to BottomBar destination from different BottomBar screen
问题:
BottomBar 有 4 个目的地:ScreenA、ScreenB、ScreenC 和 ScreenD。
在 ScreenA 上按下按钮(不是底栏项目)时,我想使用参数转到 ScreenB。
代码:
导航图
AnimatedNavHost(navController = navController, startDestination = "screen_a") {
composable("screen_a") {
ScreenA(navToScreenB = {
navController.navigate("screen_b?param=1") {
})
}
navigation(
route = "screen_b",
startDestination = "screen_b?param={param}",
) {
composable(
route = "screen_b?param={param}",
arguments = listOf(navArgument("param") { type = NavType.IntType })
) { backStackEntry ->
val param = backStackEntry.arguments?.getBoolean("param") ?: 0
ScreenB(param = param)
}
composable(route = "screen_b/details") {
ScreenB_details(...)
}
}
}
底部栏
fun BottomNavigation(navController: NavController, selectedNavigation: Screen) {
val items = listOf(...)
BottomNavigation(
backgroundColor = MaterialTheme.colors.primary
) {
items.forEach { item -> BottomNavigationItem(
onClick = {
navController.navigate(item.screen.route) {
navController.graph.startDestinationRoute?.let { route ->
popUpTo(route) { saveState = true }
}
launchSingleTop = true
restoreState = true
}
}
)
}
}
}
到目前为止,单击 ScreenA 上的按钮将我导航到 ScreenB,但参数值始终为 0,因为我永远不会传递在 navToScreenB
lambda 中传递的 arg。
我正在使用 compose_version = '1.2.0-alpha04' 和 "androidx.navigation:navigation-compose:2.5.0-alpha03"
事实证明 {param}
在路由和 navArgument({param})
中必须完全相同,即使路径看起来正确并且在路由
中有 args
问题:
BottomBar 有 4 个目的地:ScreenA、ScreenB、ScreenC 和 ScreenD。
在 ScreenA 上按下按钮(不是底栏项目)时,我想使用参数转到 ScreenB。
代码:
导航图
AnimatedNavHost(navController = navController, startDestination = "screen_a") {
composable("screen_a") {
ScreenA(navToScreenB = {
navController.navigate("screen_b?param=1") {
})
}
navigation(
route = "screen_b",
startDestination = "screen_b?param={param}",
) {
composable(
route = "screen_b?param={param}",
arguments = listOf(navArgument("param") { type = NavType.IntType })
) { backStackEntry ->
val param = backStackEntry.arguments?.getBoolean("param") ?: 0
ScreenB(param = param)
}
composable(route = "screen_b/details") {
ScreenB_details(...)
}
}
}
底部栏
fun BottomNavigation(navController: NavController, selectedNavigation: Screen) {
val items = listOf(...)
BottomNavigation(
backgroundColor = MaterialTheme.colors.primary
) {
items.forEach { item -> BottomNavigationItem(
onClick = {
navController.navigate(item.screen.route) {
navController.graph.startDestinationRoute?.let { route ->
popUpTo(route) { saveState = true }
}
launchSingleTop = true
restoreState = true
}
}
)
}
}
}
到目前为止,单击 ScreenA 上的按钮将我导航到 ScreenB,但参数值始终为 0,因为我永远不会传递在 navToScreenB
lambda 中传递的 arg。
我正在使用 compose_version = '1.2.0-alpha04' 和 "androidx.navigation:navigation-compose:2.5.0-alpha03"
事实证明 {param}
在路由和 navArgument({param})
中必须完全相同,即使路径看起来正确并且在路由