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 |
|---|
LifecycleRegistry(@NonNull LifecycleOwner 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 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 |
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 currentState)Returns the current state of the Lifecycle. |
Inherited methods |
||
|---|---|---|
|
Public constructors
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 State.STARTED state, the given observer will receive Event.ON_CREATE, Event.ON_START events.
| Parameters | |
|---|---|
@NonNull LifecycleObserver observer |
The observer to notify. |
createUnsafe
@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
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 afterwards.
| Parameters | |
|---|---|
@NonNull LifecycleObserver observer |
The observer to be removed. |
setCurrentState
public void setCurrentState(@NonNull Lifecycle.State currentState)
Returns the current state of the Lifecycle.
| Returns | |
|---|---|
void |
The current state of the Lifecycle. |