Google is committed to advancing racial equity for Black communities. See how.

LifecycleRegistry

open class LifecycleRegistry : Lifecycle
kotlin.Any
   ↳ androidx.lifecycle.Lifecycle
   ↳ androidx.lifecycle.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 constructors
<init>(@NonNull provider: LifecycleOwner)

Creates a new LifecycleRegistry for the given provider.

Public methods
open Unit
addObserver(@NonNull observer: LifecycleObserver)

open static LifecycleRegistry
createUnsafe(@NonNull owner: LifecycleOwner)

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

open Lifecycle.State

open Int

The number of observers.

open Unit

Sets the current state and notifies the observers.

open Unit
markState(@NonNull state: Lifecycle.State)

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

open Unit
removeObserver(@NonNull observer: LifecycleObserver)

open Unit

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

Public constructors

<init>

LifecycleRegistry(@NonNull 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 methods

addObserver

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

createUnsafe

@NonNull open static fun createUnsafe(@NonNull 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.

getCurrentState

@NonNull open fun getCurrentState(): Lifecycle.State

getObserverCount

open fun getObserverCount(): Int

The number of observers.

Return
Int The number of observers.

handleLifecycleEvent

open fun handleLifecycleEvent(@NonNull 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(@NonNull state: Lifecycle.State): Unit

Deprecated: Use setCurrentState(State).

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

Parameters
state Lifecycle.State: new state

removeObserver

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

setCurrentState

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

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

Parameters
state Lifecycle.State: new state