我正在尝试导航让我们说从入职到仪表板等,一旦用户点击仪表板就会弹出入职,但仍然有“后退动作”,我最终再次入职。
这是示例代码:
@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MainUI()
}
}
}
@Composable
fun MainUI() {
val navController = rememberNavController()
NavHost(
navController = navController,
startDestination = "onboarding"
) {
composable("onboarding") {
Column {
Text("I am on onboarding")
Button(onClick = {
navController.navigate("dashboard") {
popUpTo("dashboard") // I want to get rid of onboarding here
}
}) {
Text("go to dashboard")
}
}
}
composable("dashboard") {
Column {
Text("I am on dashboard")
Button(onClick = {
navController.navigate("detail")
}) {
Text("go to detail")
}
}
}
composable("detail") {
Text("I am on detail")
}
}
}
这也不起作用
navController.navigate("dashboard") {
popUpTo("dashboard") {
inclusive = true // no difference
}
// ....
popUpTo("onboarding") // also nothing
// ....
popUpTo("onboarding") {
inclusive = true // this crashes -> NavGraph cannot be cast to ComposeNavigator$Destination
}
}
出于某种原因,这种工作方式,所以仪表板被解雇,从细节上我最终入职
navController.navigate("detail") {
popUpTo("dashboard") {
inclusive = true
}
}