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


Within the earlier article, we discovered about LifeCycleCoroutineScope.launch(). Nevertheless, there are just a few further features in LifeCycleCoroutineScope:

  • launchWhenCreated()

  • launchWhenStarted()

  • launchWhenResumed()

launchWhenX()

The code utilization seems like this:

@Composable  
enjoyable DemoScreen() {  

    val lifeCycleScope = LocalLifecycleOwner.present.lifecycleScope

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

The good thing about utilizing this launchWhenX() APIs is it could actually robotically 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 features 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

Will depend on whenever you name launchWhenX(), it robotically begins the coroutine when your present lifecycle state equals or above the goal X lifecycle state.

The difficulty with launchWhenCreated() is when the lifecycle is destroyed, the LifeCycleCoroutineScope cancels all of the coroutines. Since there are not 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 comparable to its lifecycle state:

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

Given all these features above, it seems like launchWhenStarted() must be used as a result of it does not waste any sources when your app just isn’t seen. You should use launchWhenResumed(), but it surely suspends the coroutine when your app continues 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’ll be able to see, repeatOnLifecycle(Lifecycle.State.STARTED) does 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.

However, launchWhenStarted() does 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 just isn’t seen), then strikes to 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() does not restart the coroutines, it seems it’s the higher choice right here.

Conclusion

Truthfully, Android growth makes this unnecessarily complicated. Why present so many choices?

Supply Code

GitHub Repository: Demo_CoroutineScope

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles