Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: lifecycleScope.launchWhenResumed {} deprecated

I've been using lifecyclesScope's launchWhenResumed for a long time now, but it seems to be deprecated. The documentation says to use repeatOnLifecycle() but I only want the code to run once, just as it works with the old method.

like image 947
Mark George Avatar asked Dec 07 '25 06:12

Mark George


2 Answers

below code do the same functionality as viewLifeCycleOwner.launchWhenResumed

lifecycleScope.launch {
    lifecycle.repeatOnLifecycle(Lifecycle.State.RESUMED) {
        // do your work here
    }
}

update

I did more research to see what was happening under the hood of the code.

  • viewLifeCycleOwner.launchWhenResumed is a method that can be used to launch a coroutine when the associated view is resumed. When the view is paused or destroyed, the coroutine is cancelled. This method is useful when you want to start a coroutine that is specific to the view's lifecycle.
  • lifecycle.repeatOnLifecycle(Lifecycle.State.RESUMED) is a method that can be used to automatically start and stop a coroutine based on the lifecycle state of the associated LifecycleOwner. When the LifecycleOwner is resumed, the coroutine is started. When the LifecycleOwner is paused or destroyed, the coroutine is cancelled. This method is useful when you want to start a coroutine that is related to the general lifecycle of the app or a component, and not specific to a particular view.

Google recommends using lifecycle.repeatOnLifecycle(Lifecycle.State.RESUMED) over viewLifeCycleOwner.launchWhenResumed in order to promote a more efficient and flexible approach to handling lifecycle events.

one of the reasons is that it can handle background tasks more efficiently. When an app goes to the background, the LifecycleOwner associated with a view can still be in the RESUMED state, even though the view is not visible. If you use viewLifeCycleOwner.launchWhenResumed to launch a coroutine in this situation, the coroutine will continue to run even though the user has moved away from the view, which can lead to unnecessary resource consumption.

On the other hand, lifecycle.repeatOnLifecycle(Lifecycle.State.RESUMED) is designed to automatically stop coroutines when the LifecycleOwner associated with the coroutine is paused or destroyed.When the app goes to the background, the LifecycleOwner associated with the coroutine will typically be paused or destroyed, which means that the coroutine will automatically be cancelled.

like image 77
MHP Avatar answered Dec 08 '25 20:12

MHP


Create an extension function in a separate file or same

fun LifecycleOwner.launchWhenResumed(callback: () -> Unit) {
    lifecycleScope.launch { lifecycle.withResumed(callback) }
}

Usage:

launchWhenResumed { 
    // your Code..
}
like image 32
Sohaib Ahmed Avatar answered Dec 08 '25 20:12

Sohaib Ahmed



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!