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 use it directly if you have a custom LifecycleOwner.
Summary
Public constructors |
|---|
LifecycleRegistry(@NonNull LifecycleOwner provider)Creates a new LifecycleRegistry for the given provider. |
Public methods |
|
|---|---|
void |
@MainThreadAdds 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 if its methods are called on threads other than main. |
@NonNull Lifecycle.State |
Returns the current state of the Lifecycle. |
@NonNull StateFlow<@NonNull Lifecycle.State> |
Returns a |
int |
The number of observers. |
void |
Sets the current state and notifies the observers. |
void |
@MainThreadThis method is deprecated. Override [currentState]. |
void |
@MainThreadRemoves the given observer from the observers list. |
void |
setCurrentState(@NonNull Lifecycle.State state)Moves the Lifecycle to the given state and dispatches necessary events to the observers. |
Public constructors
LifecycleRegistry
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
@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 state, the given observer will receive Lifecycle.Event.ON_CREATE and Lifecycle.Event.ON_START events.
| Parameters | |
|---|---|
@NonNull LifecycleObserver observer |
The observer to notify. |
| Throws | |
|---|---|
IllegalStateException |
if no event exists to move up from the observer's initial state. |
createUnsafe
@VisibleForTesting
public static final @NonNull LifecycleRegistry createUnsafe(@NonNull LifecycleOwner owner)
Creates a new LifecycleRegistry for the given provider that doesn't check if its methods are called on 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 the main thread is not present.
getCurrentState
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.State> getCurrentStateFlow()
Returns a StateFlow where the StateFlow.value represents the current State of this Lifecycle.
| Returns | |
|---|---|
@NonNull StateFlow<@NonNull Lifecycle.State> |
|
getObserverCount
public int getObserverCount()
The number of observers.
| Returns | |
|---|---|
int |
The number of observers. |
handleLifecycleEvent
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 |
@MainThread
public voidmarkState(@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
@MainThread
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 afterward.
| Parameters | |
|---|---|
@NonNull LifecycleObserver observer |
The observer to be removed. |
setCurrentState
public void setCurrentState(@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 |