[英]Closing application using `activity. Finish()` causes malfunction on restart
I am using activity. Finish()
我正在使用activity. Finish()
activity. Finish()
to close an application on logout. activity. Finish()
在注销时关闭应用程序。 However, when the application is opened back up again, my coroutines do not launch, more specifically, a viewModelScope.launch
does not fire up.但是,当再次打开应用程序时,我的协程不会启动,更具体地说, viewModelScope.launch
不会启动。 Could this because I am closing the application wrong?这可能是因为我错误地关闭了应用程序吗? I would like to close the application and clear all viewModels and data from the application on logout我想关闭应用程序并在注销时清除应用程序中的所有 viewModels 和数据
Here is how I am closing the application这是我关闭应用程序的方式
val activity = (LocalContext.current as? Activity)
activity?. Finish()
Here is the coroutine that fails to launch on restart (user login)这是重启时无法启动的协程(用户登录)
fun login() {
Log.d("Login test", "Attempt to login stage 1")
viewModelScope.launch {
Log.d("Collection test", "Attempt to login stage 2")
userState = AuthResource.Loading
try {
} catch (e: IOException) {
} catch (e: HttpException) {
}
}
}
The first log message is received, but never the second one and the loading state is never activated.收到第一条日志消息,但从未收到第二条日志消息,并且从未激活加载 state。 I am using kotlin with jetpack compose我正在使用 kotlin 和 Jetpack Compose
When an Activity finishes, the associated view model is terminated.当 Activity 完成时,关联的视图 model 将终止。 Coroutines that haven't run yet won't be run.尚未运行的协程将不会运行。 That's part of the point of a viewModel- it's bound to a lifecycle, and that lifecycle ends with the Activity.这是 viewModel 的一部分——它绑定到一个生命周期,并且该生命周期以 Activity 结束。 You need to run it on another scope if you want to ensure it happens.如果你想确保它发生,你需要在另一个 scope 上运行它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.