LifecycleRegistry

public class LifecycleRegistry extends 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 constructors

Creates a new LifecycleRegistry for the given provider.

Public methods

void

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

static final @NonNull LifecycleRegistry

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

@NonNull Lifecycle.State

Returns the current state of the Lifecycle.

@NonNull StateFlow<@NonNull Lifecycle.State>

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

int

The number of observers.

void

Sets the current state and notifies the observers.

void

This method is deprecated. Override [currentState].

void

Removes the given observer from the observers list.

void

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

Public constructors

LifecycleRegistry

Added in 2.0.0
public LifecycleRegistry(@NonNull LifecycleOwner provider)

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
@NonNull LifecycleOwner provider

The owner LifecycleOwner

Public methods

addObserver

Added in 2.0.0
public void addObserver(@NonNull LifecycleObserver observer)

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
@NonNull LifecycleObserver observer

The observer to notify.

Throws
kotlin.IllegalStateException

if no event up from observer's initial state

createUnsafe

Added in 2.6.0
@VisibleForTesting
public static final @NonNull LifecycleRegistry createUnsafe(@NonNull LifecycleOwner owner)

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

Added in 2.0.0
public @NonNull Lifecycle.State getCurrentState()

Returns the current state of the Lifecycle.

Returns
@NonNull Lifecycle.State

The current state of the Lifecycle.

getCurrentStateFlow

public @NonNull StateFlow<@NonNull Lifecycle.StategetCurrentStateFlow()

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

Returns
@NonNull StateFlow<@NonNull Lifecycle.State>

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

getObserverCount

Added in 2.0.0
public int getObserverCount()

The number of observers.

Returns
int

The number of observers.

handleLifecycleEvent

Added in 2.0.0
public void handleLifecycleEvent(@NonNull Lifecycle.Event event)

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
@NonNull Lifecycle.Event event

The event that was received

markState

Added in 2.0.0
Deprecated in 2.1.0
@MainThread
public void markState(@NonNull Lifecycle.State state)

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

Parameters
@NonNull Lifecycle.State state

new state

removeObserver

Added in 2.0.0
public void removeObserver(@NonNull LifecycleObserver observer)

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
@NonNull LifecycleObserver observer

The observer to be removed.

setCurrentState

Added in 2.1.0
public void setCurrentState(@NonNull Lifecycle.State currentState)

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

Parameters
@NonNull Lifecycle.State currentState

new state