Hi! I have a mixed view-based and compose project....
# compose-android
p
Hi! I have a mixed view-based and compose project. I set the composable in the activity's onCreate. In the Composable I start a
LaunchedEffect
, which is polling a connected device every 5 seconds. When I put the app in the background and later bring back in the foreground, the
LaunchedEffect
starts again, but as far as I can see, the original is also still running.
Copy code
LaunchedEffect(Unit) {
    while (true) {
        viewModel.doSomething()
        delay(5.seconds)
    }
}
How can I prevent the polling duplicating itself in this case?
c
Move the polling into the viewmodel and cancel it when ithe viewmodel is cleared.
2
If you want to keep it on the UI layer - what’s anyway a bad idea - I guess if you pass the viemodel as a key to the LaunchedEffect it should also cancel the previous one.
👍 2
p
ok, I'll try, thanks!