Lifecycle

public abstract class Lifecycle
extends Object

java.lang.Object
   ↳ android.arch.lifecycle.Lifecycle


Defines an object that has an Android Lifecycle. Fragment and FragmentActivity classes implement LifecycleOwner interface which has the getLifecycle method to access the Lifecycle. You can also implement LifecycleOwner in your own classes.

ON_CREATE, ON_START, ON_RESUME events in this class are dispatched after the LifecycleOwner's related method returns. ON_PAUSE, ON_STOP, ON_DESTROY events in this class are dispatched before the LifecycleOwner's related method is called. For instance, ON_START will be dispatched after onStart returns, ON_STOP will be dispatched before onStop is called. This gives you certain guarantees on which state the owner is in.

If you use Java 8 Language, then observe events with DefaultLifecycleObserver. To include it you should add "android.arch.lifecycle:common-java8:<version>" to your build.gradle file.

 class TestObserver implements DefaultLifecycleObserver {
     @Override
     public void onCreate(LifecycleOwner owner) {
         // your code
     }
 }
 
If you use Java 7 Language, Lifecycle events are observed using annotations. Once Java 8 Language becomes mainstream on Android, annotations will be deprecated, so between DefaultLifecycleObserver and annotations, you must always prefer DefaultLifecycleObserver.
 class TestObserver implements LifecycleObserver {
   @OnLifecycleEvent(ON_STOP)
   void onStopped() {}
 }
 

Observer methods can receive zero or one argument. If used, the first argument must be of type LifecycleOwner. Methods annotated with ON_ANY can receive the second argument, which must be of type Lifecycle.Event.

 class TestObserver implements LifecycleObserver {
   @OnLifecycleEvent(ON_CREATE)
   void onCreated(LifecycleOwner source) {}
   @OnLifecycleEvent(ON_ANY)
   void onAny(LifecycleOwner source, Event event) {}
 }
 
These additional parameters are provided to allow you to conveniently observe multiple providers and events without tracking them manually.

Summary

Nested classes

enum Lifecycle.Event

 

enum Lifecycle.State

Lifecycle states. 

Public constructors

Lifecycle()

Public methods

abstract void addObserver(LifecycleObserver observer)

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

abstract Lifecycle.State getCurrentState()

Returns the current state of the Lifecycle.

abstract void removeObserver(LifecycleObserver observer)

Removes the given observer from the observers list.

Inherited methods

Public constructors

Lifecycle

Lifecycle ()

Public methods

addObserver

void addObserver (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 STARTED state, the given observer will receive ON_CREATE, ON_START events.

Parameters
observer LifecycleObserver: The observer to notify.

getCurrentState

Lifecycle.State getCurrentState ()

Returns the current state of the Lifecycle.

Returns
Lifecycle.State The current state of the Lifecycle.

removeObserver

void removeObserver (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
observer LifecycleObserver: The observer to be removed.