HomeAndroidlaunchWhenCreated() vs launchWhenStarted() vs launchWhenResumed() vs repeatOnLifeCycle()

launchWhenCreated() vs launchWhenStarted() vs launchWhenResumed() vs repeatOnLifeCycle()


Within the earlier article, we realized about LifeCycleCoroutineScope.launch(). Nonetheless, there are a number of further capabilities in LifeCycleCoroutineScope:

  • launchWhenCreated()

  • launchWhenStarted()

  • launchWhenResumed()

launchWhenX()

The code utilization appears like this:

@Composable  
enjoyable DemoScreen() {  

    val lifeCycleScope = LocalLifecycleOwner.present.lifecycleScope

    Button(onClick = {  
        lifeCycleScope.launchWhenStarted {
           /*...*/
        }
    }) 
}

The good thing about utilizing this launchWhenX() APIs is it may mechanically begin, droop and resume the coroutines for you. The desk beneath exhibits you in what lifecycle occasion, the coroutines are began, suspended, resumed and canceled.

launchWhenX capabilities When coroutines are began? When coroutines are suspended? When coroutines are resumed? When coroutines cancelled?
launchWhenCreated() ON_CREATE N/A N/A ON_DESTROY
launchWhenStarted() ON_START ON_STOP ON_START ON_DESTROY
launchWhenResumed() ON_RESUME ON_PAUSE ON_RESUME ON_DESTROY

Depends upon whenever you name launchWhenX(), it mechanically begins the coroutine when your present lifecycle state equals or above the goal X lifecycle state.

The problem with launchWhenCreated() is when the lifecycle is destroyed, the LifeCycleCoroutineScope cancels all of the coroutines. Since there aren’t any extra coroutines, there may be nothing to be suspended or resumed.

To know the lifecycle intimately, please learn the next article:

Right here is the abstract of app visibility standing akin to its lifecycle state:

Lifecycle States App Visibility Standing
CREATED NOT seen
STARTED Seen at background
RESUMED Seen at foreground

Given all these capabilities above, it appears like launchWhenStarted() must be used as a result of it would not waste any sources when your app shouldn’t be seen. You need to use launchWhenResumed(), but it surely suspends the coroutine when your app remains to be seen within the background, which we do not need.

launchWhenStarted() vs repeatOnLifeCycle()

However, wait! Is not launchWhenStarted() identical as repeatOnLifeCycle(Lifecycle.State.STARTED)?

@Composable  
enjoyable DemoScreen() {  
    val lifeCycle = LocalLifecycleOwner.present.lifecycle
    val lifeCycleScope = LocalLifecycleOwner.present.lifecycleScope

    Button(onClick = {  
        lifeCycleScope .launch {
            lifeCycle.repeatOnLifecycle(Lifecycle.State.STARTED) {
                /*...*/
            }
        }
    }) 
}

Here’s a abstract of launchWhenStarted() vs repeatOnLifecycle(Lifecycle.State.STARTED) comparisons:

Launch Capabilities When coroutines are began? When coroutines are suspended? When coroutines are resumed? When coroutines are canceled?
launchWhenStarted() ON_START ON_CREATE START ON_DESTROY
repeatOnLifecycle(Lifecycle.State.STARTED) ON_START N/A N/A ON_STOP

As you possibly can see, repeatOnLifecycle(Lifecycle.State.STARTED) would not droop or resume the coroutines. When the lifecycle state strikes beneath the STARTED state, it cancels all of the coroutines. When it strikes to STARTED state once more, it begins the coroutine once more.

Then again, launchWhenStarted() would not cancel the coroutines, but it surely suspends the coroutines as a substitute.

Launch Capabilities App Is Not Seen (ON_STOP) App Turns into Seen (ON_START)
launchWhenStarted() Coroutines suspended Coroutines resumed
repeatOnLifecycle(Lifecycle.State.STARTED) Coroutines canceled Coroutines began once more

In different phrases, what occurs if the app strikes to the background (app shouldn’t be seen), then strikes to the foreground (app is seen) once more?

  • launchWhenStarted() suspends and resumes coroutines

  • repeatOnLifecycle(Lifecycle.State.STARTED) restarts the coroutines

In case your coroutine rely from 0 10000, repeatOnLifecycle(Lifecycle.State.STARTED) merely restarts the counter and begins from 0 once more. Since launchWhenStarted() would not restart the coroutines, it seems it’s the higher choice right here.

Conclusion

Google advocates repeatOnLifecycle(Lifecycle.State.STARTED) over launchWhenStarted() as a result of launchWhenStarted() shouldn’t be secure to gather, it retains emitting within the background when the UI shouldn’t be seen. Nonetheless, I do not see this habits based mostly on the experiment that I’ve executed.

It appears to me each launchWhenStarted() and repeatOnLifecycle(Lifecycle.State.STARTED) are secure to gather. Relying in your want, launchWhenStarted() suspends and resumes the coroutine, and repeatOnLifecycle(Lifecycle.State.STARTED) restarts the coroutine.

You can even use them for gathering move. See this text for particulars:

Supply Code

GitHub Repository: Demo_CoroutineScope

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments