LifecycleRegistry



An implementation of Lifecycle that can handle multiple observers.

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

Summary

Public companion functions

LifecycleRegistry

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

Cmn
android
N

Public constructors

Cmn
android
N

Public functions

open Unit

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

Cmn
android
N
open Unit

Sets the current state and notifies the observers.

Cmn
android
N
open Unit

This function is deprecated. Override [currentState].

android
open Unit

Removes the given observer from the observers list.

Cmn
android
N

Public properties

open Lifecycle.State

Returns the current state of the Lifecycle.

Cmn
android
N
open StateFlow<Lifecycle.State>

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

android
open <Error class: unknown class><Lifecycle.State>

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

N
open Int

The number of observers.

Cmn
android
N

Inherited properties

From androidx.lifecycle.Lifecycle
open StateFlow<Lifecycle.State>

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

Cmn

Public companion functions

createUnsafe

@VisibleForTesting
fun createUnsafe(owner: LifecycleOwner): LifecycleRegistry

Creates a new LifecycleRegistry for the given provider, that doesn't check that its methods are called on the 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 main thread is not present.

Public constructors

LifecycleRegistry

LifecycleRegistry(provider: 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 State.STARTED state, the given observer will receive Event.ON_CREATE, Event.ON_START events.

Parameters
observer: LifecycleObserver

The observer to notify.

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.

currentStateFlow

open val currentStateFlow<Error class: unknown class><Lifecycle.State>

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

Returns
<Error class: unknown class><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.