You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Compose导航与ViewModel更新异步导致登录需点击两次的问题求助

Compose导航与ViewModel更新异步导致登录需点击两次的问题求助

我刚接触Android Kotlin和Jetpack Compose,最近碰到个登录相关的小麻烦:每次点登录按钮,会立刻跳转到首页,但这时候ViewModel还没完成状态更新,结果马上又被切回登录页面,害得我得连续点两次登录才能成功进入系统。

下面是我的登录页面代码:

//Login Screen
@Composable
fun LogInScreen(
    onNavigateToHomeGraph: () -> Unit
) {
    val loginViewModel = koinViewModel<HasLoggedInViewModel>()

    Column(
        modifier = Modifier.fillMaxSize()
    ) {
        Button(
            onClick = {
                successLogin(loginViewModel)
                returnToHome(onNavigateToHomeGraph)
            },
            modifier = Modifier
                .width(LocalConfiguration.current.screenWidthDp.dp / 2)
                .align(Alignment.End)
        ) {
            Text(
                "Accedi"
            )
        }
    }
}

private fun successLogin(loggedInViewModel: HasLoggedInViewModel) {
    logged...

备注:内容来源于stack exchange,提问作者nm16pc

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.16 09:08:00