LifecycleRegistry



An implementation of Lifecycle that can handle multiple observers.

It is used by Fragments and Support Library Activities. You can also use it directly if you have a custom LifecycleOwner.

Summary

Public companion functions

LifecycleRegistry

Creates a new LifecycleRegistry for the given provider that doesn't check if its methods are called on threads other than main.

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 observers.

Cmn
open Unit

This function is deprecated. Override [currentState].

Cmn
open Unit

Removes the given observer from the observers list.

Cmn

Public properties

open Lifecycle.State

Returns 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 observers.

Cmn

Public companion functions

createUnsafe

@VisibleForTesting
fun createUnsafe(owner: LifecycleOwner): LifecycleRegistry

Creates a new LifecycleRegistry for the given provider that doesn't check if its methods are called on threads other than main.

LifecycleRegistry is not synchronized: if multiple threads access this LifecycleRegistry, it must be synchronized externally.

Another possible use-case for this method is JVM testing, when the main thread is not present.

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 state, the given observer will receive Lifecycle.Event.ON_CREATE and Lifecycle.Event.ON_START events.

Parameters
observer: LifecycleObserver

The observer to notify.

Throws
IllegalStateException

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

handleLifecycleEvent

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

Sets the current state and notifies the observers.

Note that if the currentState is the same state as the last call to this method, calling this method has no effect.

Parameters
event: Lifecycle.Event

The event that was received

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 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
observer: LifecycleObserver

The observer to be removed.

Public properties

currentState

open var currentStateLifecycle.State

Returns the current state of the Lifecycle.

Returns
Lifecycle.State

The current state of the Lifecycle.

currentStateFlow

open val currentStateFlowStateFlow<Lifecycle.State>

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

Returns
StateFlow<Lifecycle.State>

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

observerCount

open val observerCountInt

The number of observers.

Returns
Int

The number of observers.