我正在尝试在 内使用 Jetpack Compose 导航viewModel。当导航被触发时,什么也不会发生。这是我的方法:
我定义NavigationDestination. 就我而言,我有四个屏幕:欢迎、登录、注册和调查
interface NavigationDestination {
val route: String
}
sealed class Screen(override val route: String) : NavigationDestination {
object WelcomeScreen : Screen("welcome")
object SignInScreen : Screen("signIn")
object SignUpScreen : Screen("signUp")
object SurveyScreen : Screen("survey")
}
Run Code Online (Sandbox Code Playgroud)
,它Navigator使用默认屏幕 公开当前目的地WelcomeScreen。
class Navigator {
var destination: MutableStateFlow<NavigationDestination> = MutableStateFlow(Screen.WelcomeScreen)
fun navigate(destination: NavigationDestination) {
this.destination.value = destination
}
}
Run Code Online (Sandbox Code Playgroud)
在主可组合项中,我获取NavHostController并监听 的更改Navigator。
@Composable
fun JetSurvey0App() {
val welcomeViewModel: WelcomeViewModel …Run Code Online (Sandbox Code Playgroud) android kotlin android-jetpack-compose jetpack-compose-navigation