The Android Developer Challenge is back! Submit your idea before December 2.

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

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