Lifecycle

public abstract class Lifecycle

Known direct subclasses
LifecycleRegistry

An implementation of Lifecycle that can handle multiple observers.


Defines an object that has an Android Lifecycle. Fragment and FragmentActivity classes implement LifecycleOwner interface which has the getLifecycle method to access the Lifecycle. You can also implement LifecycleOwner in your own classes.

ON_CREATE, ON_START, ON_RESUME events in this class are dispatched after the LifecycleOwner's related method returns. ON_PAUSE, ON_STOP, ON_DESTROY events in this class are dispatched before the LifecycleOwner's related method is called. For instance, ON_START will be dispatched after onStart returns, ON_STOP will be dispatched before onStop is called. This gives you certain guarantees on which state the owner is in.

To observe lifecycle events call addObserver passing an object that implements either DefaultLifecycleObserver or LifecycleEventObserver.

Summary

Nested types

Lifecycle.Event
Lifecycle.State

Lifecycle states.

Public constructors

Public methods

abstract @MainThread void

Adds a LifecycleObserver that will be notified when the LifecycleOwner changes state.

abstract @MainThread @NonNull Lifecycle.State

Returns the current state of the Lifecycle.

abstract @MainThread void

Removes the given observer from the observers list.

Extension functions

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

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

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

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

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

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

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

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

Public constructors

Lifecycle

public final Lifecycle()

Public methods

addObserver

@MainThread
public abstract void addObserver(@NonNull LifecycleObserver observer)

Adds a LifecycleObserver that will be notified when the LifecycleOwner changes state.

The given observer will be brought to the current state of the LifecycleOwner. For example, if the LifecycleOwner is in STARTED state, the given observer will receive ON_CREATE, ON_START events.

Parameters
@NonNull LifecycleObserver observer

The observer to notify.

getCurrentState

@MainThread
public abstract @NonNull Lifecycle.State getCurrentState()

Returns the current state of the Lifecycle.

Returns
@NonNull Lifecycle.State

The current state of the Lifecycle.

removeObserver

@MainThread
public abstract void removeObserver(@NonNull LifecycleObserver observer)

Removes the given observer from the observers list.

If this method is called while a state change is being dispatched,

  • If the given observer has not yet received that event, it will not receive it.
  • If the given observer has more than 1 method that observes the currently dispatched event and at least one of them received the event, all of them will receive the event and the removal will happen afterwards.
Parameters
@NonNull LifecycleObserver observer

The observer to be removed.

Extension functions

WithLifecycleStateKt.withCreated

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

Run block with this 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

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

Run block with this 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

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

Run block with this 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

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

Run block with this 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.