LifecycleKt

Added in 2.2.0

public final class LifecycleKt


Summary

Public methods

static final @NonNull LifecycleObserver

Add 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 containing values dispatched by this Lifecycle.

Public methods

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

Add a LifecycleObserver to this Lifecycle using the provided action.

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

The action that will be invoked whenever a Lifecycle.Event occurs. It provides the LifecycleOwner whose state has changed and the specific Lifecycle.Event that was triggered.

Returns
@NonNull LifecycleObserver

the LifecycleObserver added to the Lifecycle. This instance can be used to later remove the observer.

getCoroutineScope

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

CoroutineScope tied to this Lifecycle.

This scope will be cancelled when the Lifecycle is destroyed.

This scope is bound to Dispatchers.Main.immediate

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

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