In jetpack compose mvvm, how to update screen cont...
# compose-android
p
In jetpack compose mvvm, how to update screen content each 5 seconds only when the screen is in foreground? doing it in a while(isActive) in the init block of the viewmodel doesn't work, because it still does the job when the screen is in background Is it a good practice and the best option to do it using this code inside the screen composable? (seems to work perfectly)
Copy code
val lifecycleOwner = LocalLifecycleOwner.current
    LaunchedEffect(lifecycleOwner) {
        lifecycleOwner.lifecycle.repeatOnLifecycle(Lifecycle.State.RESUMED) {
            while (isActive) {
                vm.populateData()
                delay(5_000) // every 5 seconds
            }
        }
    }
f
do you use
collectAsStateWithLifecycle()
?
p
Copy code
val uiState by vm.uiState.collectAsStateWithLifecycle()
this?
f
Yes, the view is only updated when active
(By default)