Google 致力于为黑人社区推动种族平等。查看具体举措

LifecycleCoroutineScope

abstract class LifecycleCoroutineScope : CoroutineScope
kotlin.Any
   ↳ androidx.lifecycle.LifecycleCoroutineScope

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 methods
Job
launchWhenCreated(block: suspend CoroutineScope.() -> Unit)

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

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

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

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

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

Public methods

launchWhenCreated

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

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

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.