Skip to content

Most visited

Recently visited

navigation

Lifecycle

public abstract class Lifecycle
extends Object

java.lang.Object
   ↳ android.arch.lifecycle.Lifecycle
Known Direct Subclasses


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.

Lifecycle events are observed using annotations.

 class TestObserver implements LifecycleObserver {
   @OnLifecycleEvent(ON_STOP)
   void onStopped() {}
 }
 

Multiple methods can observe the same event.

 class TestObserver implements LifecycleObserver {
   @OnLifecycleEvent(ON_STOP)
   void onStoppedFirst() {}
   @OnLifecycleEvent(ON_STOP)
   void onStoppedSecond() {}
 }
 

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

From class java.lang.Object

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.

This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a one-minute survey?
Help us improve Android tools and documentation.