https://kotlinlang.org logo
#android
Title
# android
a

althaf

08/21/2022, 4:32 AM
hi , It ok to do this in a single launch ? or do we have to use separate launch for repeat on life cylce ? lifecycleScope.launch { lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) { loginAuthorizationViewModel.onLoginSuccessful.collect { startActivity(HomeActivity.getDefaultIntent(this@LoginActivity)) finish() } } lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) { loginInputValidationViewModel.onPasswordInvalid.collect { enableViews(true) et_password.error = it } } lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) { loginAuthorizationViewModel.onLoginFailure.collect { enableViews(true) showMessage(it.toString()) } } lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) { loginInputValidationViewModel.onUserNameInvalid.collect { enableViews(true) tv_user_name.error = it } } lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) { loginInputValidationViewModel.onValidationSuccessful.collect { loginAuthorizationViewModel.requestLogin( userName = it.first, password = it.second ) } }
🧵 3
j

Javier

08/21/2022, 10:57 AM
I think under the hood
repeatOnLifecycle
launch its own coroutines
a

althaf

08/21/2022, 4:30 PM
@Javier my trial shows it doesn't work. I feel there are better ways to do this, however i just don't know how.
we have to use different launch
a

agrosner

08/21/2022, 5:02 PM
Separate launches within repeat on lifecycle
Call launch on all inner suspends
g

gian

08/22/2022, 9:05 AM
Inside
repeatOnLifecycle
, for every flow you can do
flow.onEach { do something }.launchIn(this)
(where
this
is the coroutine scope). It is equivalent to
launch { flow.collect { do something } }
4 Views