LifecycleKt

Added in 2.6.0

public final class LifecycleKt


Summary

Public methods

static final @NonNull LifecycleObserver

Adds a LifecycleObserver to this Lifecycle using the provided action.

static final @NonNull LifecycleCoroutineScope

CoroutineScope tied to this Lifecycle.

static final @NonNull Flow<@NonNull Lifecycle.Event>

Creates a Flow of Lifecycle.Events dispatched by this Lifecycle.

Public methods

addObserver

public static final @NonNull LifecycleObserver addObserver(
    @NonNull Lifecycle receiver,
    @NonNull Function3<@NonNull LifecycleObserver, @NonNull LifecycleOwner, @NonNull Lifecycle.EventUnit> action
)

Adds a LifecycleObserver to this Lifecycle using the provided action.

Invokes action whenever a Lifecycle.Event occurs.

Parameters
@NonNull Function3<@NonNull LifecycleObserver, @NonNull LifecycleOwner, @NonNull Lifecycle.EventUnit> action

The action invoked on each Lifecycle.Event, providing the LifecycleOwner and the specific Lifecycle.Event.

Returns
@NonNull LifecycleObserver

the added LifecycleObserver instance (can be used to later remove it).

getCoroutineScope

public static final @NonNull LifecycleCoroutineScope getCoroutineScope(@NonNull Lifecycle receiver)

CoroutineScope tied to this Lifecycle.

Canceled when the Lifecycle is destroyed. Bound to Dispatchers.Main.immediate.

getEventFlow

public static final @NonNull Flow<@NonNull Lifecycle.EventgetEventFlow(@NonNull Lifecycle receiver)

Creates a Flow of Lifecycle.Events dispatched by this Lifecycle.