{% setvar book_path %}/reference/androidx/_book.yaml{% endsetvar %} {% include "_shared/_reference-head-tags.html" %}

LifecycleOwner

public interface LifecycleOwner

Known direct subclasses
LifecycleRegistryOwner

This interface is deprecated.

Use androidx.appcompat.app.AppCompatActivity which extends LifecycleOwner, so there are no use cases for this class.

LifecycleService

A Service that is also a LifecycleOwner.

ProcessLifecycleOwner

Class that provides lifecycle for the whole application process.

TestLifecycleOwner

Create a LifecycleOwner that allows changing the state via the handleLifecycleEvent method or currentState property.


A class that has an Android lifecycle. These events can be used by custom components to handle lifecycle changes without implementing any code inside the Activity or the Fragment.

See also
Lifecycle
ViewTreeLifecycleOwner

Summary

Public methods

abstract @NonNull Lifecycle

Returns the Lifecycle of the provider.

Extension functions

default final @NonNull LifecycleCoroutineScope

CoroutineScope tied to this LifecycleOwner's Lifecycle.

default final @NonNull R
<R extends Object> WithLifecycleStateKt.withCreated(
    @NonNull LifecycleOwner receiver,
    @NonNull Function0<@NonNull R> block
)

Run block with this LifecycleOwner's Lifecycle in a Lifecycle.State of at least Lifecycle.State.CREATED and resume with the result.

default final @NonNull R
<R extends Object> WithLifecycleStateKt.withResumed(
    @NonNull LifecycleOwner receiver,
    @NonNull Function0<@NonNull R> block
)

Run block with this LifecycleOwner's Lifecycle in a Lifecycle.State of at least Lifecycle.State.RESUMED and resume with the result.

default final @NonNull R
<R extends Object> WithLifecycleStateKt.withStarted(
    @NonNull LifecycleOwner receiver,
    @NonNull Function0<@NonNull R> block
)

Run block with this LifecycleOwner's Lifecycle in a Lifecycle.State of at least Lifecycle.State.STARTED and resume with the result.

default final @NonNull R
<R extends Object> WithLifecycleStateKt.withStateAtLeast(
    @NonNull LifecycleOwner receiver,
    @NonNull Lifecycle.State state,
    @NonNull Function0<@NonNull R> block
)

Run block with this LifecycleOwner's Lifecycle in a Lifecycle.State of at least state and resume with the result.

Public methods

getLifecycle

abstract @NonNull Lifecycle getLifecycle()

Returns the Lifecycle of the provider.

Returns
@NonNull Lifecycle

The lifecycle of the provider.

Extension functions

LifecycleOwnerKt.getLifecycleScope

default final @NonNull LifecycleCoroutineScope LifecycleOwnerKt.getLifecycleScope(@NonNull LifecycleOwner receiver)

CoroutineScope tied to this LifecycleOwner's Lifecycle.

This scope will be cancelled when the Lifecycle is destroyed.

This scope is bound to Dispatchers.Main.immediate.

WithLifecycleStateKt.withCreated

default final @NonNull R <R extends Object> WithLifecycleStateKt.withCreated(
    @NonNull LifecycleOwner receiver,
    @NonNull Function0<@NonNull R> block
)

Run block with this LifecycleOwner's Lifecycle in a Lifecycle.State of at least Lifecycle.State.CREATED and resume with the result. Throws the CancellationException if the lifecycle has reached Lifecycle.State.DESTROYED by the time of the call or before block is able to run.

WithLifecycleStateKt.withResumed

default final @NonNull R <R extends Object> WithLifecycleStateKt.withResumed(
    @NonNull LifecycleOwner receiver,
    @NonNull Function0<@NonNull R> block
)

Run block with this LifecycleOwner's Lifecycle in a Lifecycle.State of at least Lifecycle.State.RESUMED and resume with the result. Throws the CancellationException if the lifecycle has reached Lifecycle.State.DESTROYED by the time of the call or before block is able to run.

WithLifecycleStateKt.withStarted

default final @NonNull R <R extends Object> WithLifecycleStateKt.withStarted(
    @NonNull LifecycleOwner receiver,
    @NonNull Function0<@NonNull R> block
)

Run block with this LifecycleOwner's Lifecycle in a Lifecycle.State of at least Lifecycle.State.STARTED and resume with the result. Throws the CancellationException if the lifecycle has reached Lifecycle.State.DESTROYED by the time of the call or before block is able to run.

WithLifecycleStateKt.withStateAtLeast

default final @NonNull R <R extends Object> WithLifecycleStateKt.withStateAtLeast(
    @NonNull LifecycleOwner receiver,
    @NonNull Lifecycle.State state,
    @NonNull Function0<@NonNull R> block
)

Run block with this LifecycleOwner's Lifecycle in a Lifecycle.State of at least state and resume with the result. Throws the CancellationException if the lifecycle has reached Lifecycle.State.DESTROYED by the time of the call or before block is able to run.