LifecycleCoroutineScope

abstract class LifecycleCoroutineScope : CoroutineScope


CoroutineScope tied to a Lifecycle and Dispatchers.Main.immediate

This scope will be cancelled when the Lifecycle is destroyed.

This scope provides specialised versions of launch: launchWhenCreated, launchWhenStarted, launchWhenResumed

Summary

Public functions

Job
launchWhenCreated(block: suspend CoroutineScope.() -> Unit)

This function is deprecated. launchWhenCreated is deprecated as it can lead to wasted resources in some cases.

Job
launchWhenResumed(block: suspend CoroutineScope.() -> Unit)

This function is deprecated. launchWhenResumed is deprecated as it can lead to wasted resources in some cases.

Job
launchWhenStarted(block: suspend CoroutineScope.() -> Unit)

This function is deprecated. launchWhenStarted is deprecated as it can lead to wasted resources in some cases.

Public functions

launchWhenCreated

Added in 2.2.0
fun launchWhenCreated(block: suspend CoroutineScope.() -> Unit): Job

Launches and runs the given block when the Lifecycle controlling this LifecycleCoroutineScope is at least in Lifecycle.State.CREATED state.

The returned Job will be cancelled when the Lifecycle is destroyed.

launchWhenResumed

Added in 2.2.0
fun launchWhenResumed(block: suspend CoroutineScope.() -> Unit): Job

Launches and runs the given block when the Lifecycle controlling this LifecycleCoroutineScope is at least in Lifecycle.State.RESUMED state.

The returned Job will be cancelled when the Lifecycle is destroyed.

launchWhenStarted

Added in 2.2.0
fun launchWhenStarted(block: suspend CoroutineScope.() -> Unit): Job

Launches and runs the given block when the Lifecycle controlling this LifecycleCoroutineScope is at least in Lifecycle.State.STARTED state.

The returned Job will be cancelled when the Lifecycle is destroyed.