Class Lifecycle
- Direct Known Subclasses:
LifecycleRegistry
Fragment
class implement LifecycleOwner
interface which has the
LifecycleOwner.getLifecycle()
method to access the Lifecycle. You can also
implement LifecycleOwner
in your own classes.
Lifecycle.Event.ON_CREATE
, Lifecycle.Event.ON_START
, Lifecycle.Event.ON_RESUME
events in this class
are dispatched after the LifecycleOwner
's related method returns.
Lifecycle.Event.ON_PAUSE
, Lifecycle.Event.ON_STOP
, Lifecycle.Event.ON_DESTROY
events in this class
are dispatched before the LifecycleOwner
's related method is called.
This gives you certain guarantees on which state the owner is in.
Observe lifecycle events with LifecycleObserver
.
class TestObserver implements LifecycleObserver { @Override public void onCreate(@Nonnull LifecycleOwner owner) { // your code } }
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic enum
static enum
Lifecycle states. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionabstract void
addObserver
(LifecycleObserver observer) Adds a LifecycleObserver that will be notified when the LifecycleOwner changes state.abstract Lifecycle.State
Returns the current state of the Lifecycle.abstract void
removeObserver
(LifecycleObserver observer) Removes the given observer from the observers list.
-
Constructor Details
-
Lifecycle
public Lifecycle()
-
-
Method Details
-
addObserver
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 receiveLifecycle.Event.ON_CREATE
,Lifecycle.Event.ON_START
events.- Parameters:
observer
- The observer to notify.
-
removeObserver
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:
observer
- The observer to be removed.
-
getCurrentState
Returns the current state of the Lifecycle.- Returns:
- The current state of the Lifecycle.
-