LifecycleRegistry

open class LifecycleRegistry : Lifecycle


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.

Public constructors

Creates a new LifecycleRegistry for the given provider.

Public functions

open Unit

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

open Unit

Sets the current state and notifies the observers.

open Unit

This function is deprecated. Override [currentState].

open Unit

Removes the given observer from the observers list.

Public properties

open Lifecycle.State

Returns the current state of the Lifecycle.

open StateFlow<Lifecycle.State>

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

open Int

The number of observers.

Public companion functions

createUnsafe

Added in 2.6.0
@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

Added in 2.0.0
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

Added in 2.0.0
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, Lifecycle.Event.ON_START events.

Parameters
observer: LifecycleObserver

The observer to notify.

Throws
kotlin.IllegalStateException

if no event up from observer's initial state

handleLifecycleEvent

Added in 2.0.0
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

Added in 2.0.0
Deprecated in 2.1.0
@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

Added in 2.0.0
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

Added in 2.0.0
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

Added in 2.0.0
open val observerCountInt

The number of observers.

Returns
Int

The number of observers.