LifecycleRegistry


public class LifecycleRegistry extends Lifecycle


A Lifecycle implementation that manages multiple LifecycleObservers.

Commonly used by UI containers (like Activities or Fragments on Android) to manage component lifecycles. Can be used directly to implement custom LifecycleOwner components.

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 LifecycleOwner without main-thread enforcement.

@NonNull Lifecycle.State

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 registered LifecycleObservers.

void

Sets the current State and notifies the LifecycleObservers.

void

This method is deprecated. Override [currentState].

void

Removes the given observer from the list of registered LifecycleObservers.

void

The current State of the Lifecycle.

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
@MainThread
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, the given observer receives Lifecycle.Event.ON_CREATE and Lifecycle.Event.ON_STARTs.

Parameters
@NonNull LifecycleObserver observer

The LifecycleObserver to notify.

Throws
IllegalStateException

if no event exists to move up from the observer's initial state.

IllegalStateException

if main-thread enforcement is enabled and called on a thread other than the main thread.

createUnsafe

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

Creates a new LifecycleRegistry for the given LifecycleOwner without main-thread enforcement.

Note: LifecycleRegistry is not thread-safe. Multiple threads accessing it must synchronize externally. Useful for JVM testing where a main thread dispatcher is absent.

getCurrentState

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

The current State of the Lifecycle.

Transitions the Lifecycle to the given target State and dispatches the corresponding Events to any registered LifecycleObservers.

Throws
IllegalStateException

if main-thread enforcement is enabled and called on a thread other than the main thread.

getCurrentStateFlow

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

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

getObserverCount

Added in 2.0.0
public int getObserverCount()

The number of registered LifecycleObservers.

Throws
IllegalStateException

if main-thread enforcement is enabled and called on a thread other than the main thread.

handleLifecycleEvent

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

Sets the current State and notifies the LifecycleObservers.

No-ops if the target state of the event matches currentState.

Parameters
@NonNull Lifecycle.Event event

The Event to process.

Throws
IllegalStateException

if main-thread enforcement is enabled and called on a thread other than the main thread.

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
@MainThread
public void removeObserver(@NonNull LifecycleObserver observer)

Removes the given observer from the list of registered LifecycleObservers.

Parameters
@NonNull LifecycleObserver observer

The LifecycleObserver to remove.

Throws
IllegalStateException

if main-thread enforcement is enabled and called on a thread other than the main thread.

setCurrentState

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

The current State of the Lifecycle.

Transitions the Lifecycle to the given target State and dispatches the corresponding Events to any registered LifecycleObservers.

Throws
IllegalStateException

if main-thread enforcement is enabled and called on a thread other than the main thread.