在将导航撰写依赖从2.4.0-alpha03更新到2.4.0-alpha05后,我在尝试在可组合屏幕之间导航后遇到了这个奇怪的错误(例如从taskComposable导航到listComposable屏幕)。这是我的一部分代码。
主要活动:
@AndroidEntryPoint
class MainActivity : ComponentActivity() {
private lateinit var navController: NavHostController
@ExperimentalAnimationApi
@ExperimentalMaterialApi
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MyComposeDemoTheme {
navController = rememberNavController()
SetupNavigation(
navController = navController
)
}
}
}
}
设置导航:
@ExperimentalAnimationApi
@ExperimentalMaterialApi
@Composable
fun SetupNavigation(
navController: NavHostController
) {
val screen = remember(navController) { Screens(navController) }
NavHost(
navController = navController,
startDestination = "list/{action}"
) {
listComposable(
navigateToTaskScreen = screen.task
)
taskComposable(
navigateToListScreen = screen.list
)
}
}
屏幕:
class Screens(navController: NavHostController) {
val list: (Action) -> Unit = { action ->
navController.navigate("list/${action.name}"){
popUpTo(LIST_SCREEN) { inclusive = true }
}
}
val task: (Int) -> Unit = { taskId ->
navController.navigate("task/$taskId")
}
}
taskComposable
fun NavGraphBuilder.taskComposable(
navigateToListScreen: (Action) -> Unit
) {
composable(
route = TASK_SCREEN,
arguments = listOf(navArgument(TASK_ID_ARGUMENT_KEY) {
type = NavType.IntType
})
) {
TaskScreen(
navigateToListScreen = navigateToListScreen
)
}
}
错误:
这是一个内部bug,将在导航2.4.0-alpha06版本中修复。@header svk分享了问题链接:https://issuetracker.google.com/issues/194301889