Android Architecture Components: LifeCycle

Mina Samy
4 min readFeb 23, 2018

--

In a previous post we talked about Architecture components:

Now we’ll explore the first component: LifeCycle.

The LifeCycle component is concerned with the Android LifeCycle events of a component such an Activity or a Fragment, it has three main classes that we’ll deal with:

LifeCycle

A LifeCycle object is an object that can retrieve information about the current lifecycle of an LifeCycleOwner, typically the owner would be an Activity or a fragment.

The LifeCycle objects provides information about the lifecycle of the owner in the terms of Events and States.

Events are dispatched with respect to the life cycle owner events such as onCreate(), onStart(), onStop(), …

LifeCycle events timeline

From the above timeline illustration we notice that the events ON_CREATE, ON_START, ON_RESUME are dispatched just after the corresponding Activity’s (LifeCycleOwner) onCreate(), onStart() and onResume() return.

Also we notice that there is a special event: ON_ANY which matches all the lifecycle owner events.

States as described in the docs:

Lifecycle states. You can consider the states as the nodes in a graph and Lifecycle.Events as the edges between these nodes.

I also like to think of them as values that describe the life cycle state of a lifecycle owner at a given point of time

Lifecycle states timeline

From the above illustration we start with the INITIALIZED state which represents the stat where the lifecycle owner is constructed but still didn’t receive its onCreate() yet.

We also notice that a single state can span multiple lifecycle owner events, so for an activity it’s considered in the CREATED state once it’s created and just before it’s paused, and in the same time the duration between the activity’s onStart() and just before onPause() it’s considered also in the STARTED state.

Since multiple states can interleave for a given point of time, if we want to check for a specific state, we always use the isAtLeast method:

if (lifeCycle.currentState.isAtLeast(Lifecycle.State.STARTED)) {
//...
}

LifeCycleOwner

Any class that implements the LifeCycleOwner interface indicates that it has Android LifeCycle.

For example, starting from Support Library 26.1.0 Fragments and Activities implement the LifeCycleOwner interface.

We can create custom LifeCycleOwner components by implementing the interface and using a LifeCycleRegistry as described here.

LifeCycleObserver

It’s a marker interface that indicates that the implementing interface is a LifeCycleObserver. Interaction with the LifeCycle Events can be done in two ways:

  • If we’re using Java 8, an observer class can implement the DefaultLifecycleObserver (which in turn implements the LifecycleObserver ) interface which defines the life cycle events as default methods, so we can override the methods that represent the events we’re interested in:
class MyLifeCycleObserver : DefaultLifecycleObserver {

override fun onCreate(owner: LifecycleOwner) {
//...
}
}
class MyLifeCycleObserver(val lifeCycle: Lifecycle) : LifecycleObserver {@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
fun onCreated() {
//...
}
}

Now we will implement a simple application to show how we can use the LifeCycle components, it will be a very simple app that logs the different life cycle events that our main activity goes through.

Dependencies:

All explained here, for now we’ll keep it simple by adding lifecycle components only:

dependencies {
// Lifecycles only (no ViewModel or LiveData)
implementation "android.arch.lifecycle:runtime:1.1.0"
annotationProcessor "android.arch.lifecycle:compiler:1.1.0"
}

First we’ll create a simple Logger class:

Then we’ll create our LifeCycleObserver class that wraps the logger to log the different life cycle events:

Here we’re interacting with the LifeCycle Events through the OnLifecycleEvent annotation.

Then in our Main Activity (which is our LifeCycleOwner) we add our observer to the activity’s life cycle observers:

And finally let’s add some unit tests:

Here we used a LifeCycleRegistry as a LifeCycleOwner, also in testing onStop() life cycle event, we had to move the life cycle to the previous state (onResume()) before bringing it to the ON_STOP state, that’s necessary for the life cycle events that are dispatched before the actual LifeCycleOwner events return such as ON_STOP and ON_PAUSE.

And that’s it for our simple example, full source code can be found on GitHub.

Thank you.

--

--