What is Lifecycle and why should I care?

Lifecycle is a part of many Architecture Components released by Google to make the life of Android devs a whole lot easier. Lifecycle provides a set of classes and interfaces that let you better mange your application components according to your activity/fragment lifecycle. Also you need to understand how Lifecycle works before learning a very handy architecture component, LiveData.

There are a couple of good different examples provided in the official docs on Lifecycle that you should read after going through this article. This article aims to give you a simple understanding of how this component works.

Undestanding the Lifecycle Components

The basic idea here is that there is a class named LifecycleOwner which emits various lifecycle events (such as ON_CREATE, ON_PAUSE, etc) which you as an Android Developer are already familiar with. Now you can implement an interface name LifecycleObserver which listens to these events and respond accordingly.

Right now you don’t need to worry about how to create a LifecycleOwner. Your activity and fragment already implement this interface.

There are two different enums to track the lifecycle of a component, Event and State.

Event

Event is the current lifecycle that ‘happened’. So suppose your activity goes into onPause, so an event named ON_PAUSE will be fired, when it goes into onStop an event named ON_STOP will be fired. You can register to listen to these events and take some action (we will shortly see an example which will makes things clear). Here is the Event  enum class from the SDK, with all the defined events.

State

State is as the name says, current state in which the component is. For example, when onCreate is called the component is in CREATED state, when onStart is called it is in STARTED state. Here is the State  enum from SDK.

You can see there is a function named  isAtLeast which returns a boolean. So if the current state is CREATED and you want to check if the component has started, then you will call isAtLeast(State.STARTED) which in this case will return false .

Below is an image from official docs that shows Event and State.

lifecycle

Never miss a post


Let’s code lifecycle

Now lets see Lifecycle in action. Code along with me!

Open a project and create an empty activity. As I mentioned earlier, to observe lifecycle events emitted by LifecycleObserver we will have to implement an interface named LifecycleObserver. Here is the interface definition from the SDK.

The interface contains no functions :P. So how are you supposed to listen to events? We will do that using annotations. Lets create a class named CustomObserver which will implement LifecycleObserver . Lets listen to the onCreate event of the activity, in the class create a method and name it whatever you want, I will name it…….onCreateEvent and annotate it with  @OnLifecycleEvent . In the annotation you specify which event you want to listen to, so we will pass  Lifecycle.Event.ON_CREATE in the annotation.

You can write as many functions you want and listen to as many events you want as many times you want.

Now we need to register this LifecycleObserver  to the LifecycleOwner so that we can listen to the events. Now before doing that, you might be wondering, where does this LifecycleOwner comes from??? Remember I told you that Activity and Fragment already provide an implementation of LifecycleOwner .

If you go up the inheritance level of AppCompatActivity till you find SupportActivity , You can see that SupportActivity implements LifecycleOwner.

Here is the LifecycleOwner interface from SDK containing only one method,  getLifecycle() which in turn returns an object of  Lifecycle .

If you dig deep and look into Lifecycle you can see that it is an abstract class containing various methods and the Event  and Status enum classes that we talked about earlier (I have removed all the doc comments for the sake of simplicity). One of the method you can see is addObserver()  which takes a LifecycleObserver as a parameter. This is exactly what we are looking for. We will register our CustomObserver using this function.

Now in the onCreate of your activity get the Lifecycle by calling getLifecycle() and add the object of your new observer by calling addObserver.

Run your application and check the logs. Experiment more with this LifecycleObserver class.

You can also implement your custom LifecycleOwner  and send events using LifecycleRegistry . It is quite straightforward and explained in the docs here, so I won’t go over it.

Lifecycle Component is a convenient class which lays foundation on understanding the workings of LiveData (which we will take a look at in next tutorial). I would recommend you to now read the docs on Lifecycle.

Get stared with Room Database in Android

Kotlin Data Class Tutorial

Move Content to side while swiping in Navigation Drawer

Using Fonts in Android


Gurleen Sethi

How can I tell you about myself in just a few lines, to get to know me you can reach out to me, email, text, call, or any other method of communication you like. I am always there for you!

2 Comments

Kashish · August 10, 2018 at 7:52 am

So is this just for understanding or do we actually need this somewhere ??
What IAM understanding is that it’s just another way to keep our code clean and all the loggings we used to do in our activities are now done in interface itself.

    Gurleen Sethi · August 10, 2018 at 10:44 pm

    LiveData heavily relies on Lifecycle so you need to understand Lifecycle before going ahead.

Leave a Reply

Your email address will not be published. Required fields are marked *

7 − four =