{% setvar book_path %}/reference/kotlin/androidx/_book.yaml{% endsetvar %} {% include "_shared/_reference-head-tags.html" %}

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 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 Int

The number of observers.

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)

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

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

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

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,

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.

observerCount

open val observerCountInt

The number of observers.

Returns
Int

The number of observers.