How to launch a coroutine in IO that can pause when lifecyle is not atleast in STARTED state

Viewed 372

Using lifecycleScope.launchWhenStarted I can start a coroutine that will pause if my activity is not in atleast started state, and will resume if when the activity goes back in started state.

I want to replicate this behavior on a coroutine that is running on IO. I tried this, but it doesn't work:

lifecycleScope.launchWhenStarted {
    withContext(IO)
    {
        var i = 0
        while (true)
        {
            Log.i("launchWhenStarted", "STATE: ${lifecycle.currentState.name}  ${i++}")
            delay(500)
        }
    }
}
0 Answers
Related