LifecycleRegistry



A Lifecycle implementation that manages multiple LifecycleObservers.

Commonly used by UI containers (like Activities or Fragments on Android) to manage component lifecycles. Can be used directly to implement custom LifecycleOwner components.

Summary

Public companion functions

LifecycleRegistry

Creates a new LifecycleRegistry for the given LifecycleOwner without main-thread enforcement.

Cmn

Public constructors

Creates a new LifecycleRegistry for the given provider.

Cmn

Public functions

open Unit

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

Cmn
open Unit

Sets the current State and notifies the LifecycleObservers.

Cmn
open Unit

This function is deprecated. Override [currentState].

Cmn
open Unit

Removes the given observer from the list of registered LifecycleObservers.

Cmn

Public properties

open Lifecycle.State

The current State of the Lifecycle.

Cmn
open StateFlow<Lifecycle.State>

Returns a StateFlow where the StateFlow.value represents the current State of this Lifecycle.

Cmn
open Int

The number of registered LifecycleObservers.

Cmn

Public companion functions

createUnsafe

@VisibleForTesting
fun createUnsafe(owner: LifecycleOwner): LifecycleRegistry

Creates a new LifecycleRegistry for the given LifecycleOwner without main-thread enforcement.

Note: LifecycleRegistry is not thread-safe. Multiple threads accessing it must synchronize externally. Useful for JVM testing where a main thread dispatcher is absent.

Public constructors

LifecycleRegistry

LifecycleRegistry(provider: LifecycleOwner)

Creates a new LifecycleRegistry for the given provider.

You should usually create this inside your LifecycleOwner class's constructor and hold onto the same instance.

Parameters
provider: LifecycleOwner

The owner LifecycleOwner

Public functions

addObserver

@MainThread
open fun addObserver(observer: LifecycleObserver): Unit

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 Lifecycle.State.STARTED, the given observer receives Lifecycle.Event.ON_CREATE and Lifecycle.Event.ON_STARTs.

Parameters
observer: LifecycleObserver

The LifecycleObserver to notify.

Throws
IllegalStateException

if no event exists to move up from the observer's initial state.

IllegalStateException

if main-thread enforcement is enabled and called on a thread other than the main thread.

handleLifecycleEvent

open fun handleLifecycleEvent(event: Lifecycle.Event): Unit

Sets the current State and notifies the LifecycleObservers.

No-ops if the target state of the event matches currentState.

Parameters
event: Lifecycle.Event

The Event to process.

Throws
IllegalStateException

if main-thread enforcement is enabled and called on a thread other than the main thread.

markState

@MainThread
open fun markState(state: Lifecycle.State): Unit

Moves the Lifecycle to the given state and dispatches necessary events to the observers.

Parameters
state: Lifecycle.State

new state

removeObserver

@MainThread
open fun removeObserver(observer: LifecycleObserver): Unit

Removes the given observer from the list of registered LifecycleObservers.

Parameters
observer: LifecycleObserver

The LifecycleObserver to remove.

Throws
IllegalStateException

if main-thread enforcement is enabled and called on a thread other than the main thread.

Public properties

currentState

open var currentStateLifecycle.State

The current State of the Lifecycle.

Transitions the Lifecycle to the given target State and dispatches the corresponding Events to any registered LifecycleObservers.

Throws
IllegalStateException

if main-thread enforcement is enabled and called on a thread other than the main thread.

currentStateFlow

open val currentStateFlowStateFlow<Lifecycle.State>

Returns a StateFlow where the StateFlow.value represents the current State of this Lifecycle.

observerCount

open val observerCountInt

The number of registered LifecycleObservers.

Throws
IllegalStateException

if main-thread enforcement is enabled and called on a thread other than the main thread.