LifecycleCoroutineScope

public abstract class 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 fields

abstract @NonNull CoroutineContext

Public methods

final @NonNull Job

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

final @NonNull Job

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

final @NonNull Job

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

Public fields

coroutineContext

@NonNull
public abstract @NonNull CoroutineContext coroutineContext

Public methods

launchWhenCreated

@NonNull
public final Job launchWhenCreated(
    @NonNull SuspendFunction1<@NonNull CoroutineScopeUnit> block
)

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.

Caution: This API is not recommended to use as it can lead to wasted resources in some cases. Please, use the Lifecycle.repeatOnLifecycle API instead. This API will be removed in a future release.

launchWhenResumed

@NonNull
public final Job launchWhenResumed(
    @NonNull SuspendFunction1<@NonNull CoroutineScopeUnit> block
)

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.

Caution: This API is not recommended to use as it can lead to wasted resources in some cases. Please, use the Lifecycle.repeatOnLifecycle API instead. This API will be removed in a future release.

launchWhenStarted

@NonNull
public final Job launchWhenStarted(
    @NonNull SuspendFunction1<@NonNull CoroutineScopeUnit> block
)

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.

Caution: This API is not recommended to use as it can lead to wasted resources in some cases. Please, use the Lifecycle.repeatOnLifecycle API instead. This API will be removed in a future release.