Lifecycle for Android Developers (Part 1)

Dinorah Tovar
5 min readJan 31, 2022

Under the hood of lifecycleOwners, collection, and events

Here is the list of the blogs in this series:

Flows

This is the first part of this series “Lifecycle for Android Developers” in this series we will discuss the magical world of lifecycle and what does this mean for android applications — in the last two part of this series we will cover how the lifecycle works on Composable functions and we will discuss why State Hoisting is so important when we talk about declarative UI, in Part 3 — we will discuss Flows (SharedFlow and Stateflow) and how we can use lifecycle to handle the disposable process

First, let’s talk about what it means to have a lifecycle, we know that Fragments and Activities has a lifecycle — so this means applications also have a lifecycle, as developers, we can obtain the state of the lifecycle of our application using multiple callbacks or even annotation functions

All these elements on Android can extend an interface called LifecycleOwner, for example, the Fragments and Activities are already aware of this interface, which has a way to access the lifecycle using getLifecycle() method to access the Lifecycle, so this means that we can create our own classes to track our lifecycle, don’t forget to be careful with how you access them — If you are looking for an elegant way to catch if your app is on Foreground or Background you can check this very old blog post that probably is deprecated in functionality, whatsoever it does the job and you may be able to update after this serial

We are not gonna cover all the lifecycle callbacks, however, we are gonna talk about two important elements in the Lifecycle — States and Events — we are also gonna talk about LifecycleOwner and some functions that are used daily on our mobile applications

Lifecycle

Lifecycle is an abstract Class on Android that defines an object that has an Android Lifecycle, inside this class, we can find two enums called

public enum Event { /* Code for events */ }
public enum State { /* Code for states */ }

There are also two important functions that we will address in the second part that are in charge of Observers — that are commonly used on LiveData and Flows

public abstract void addObserver
public abstract void removeObserver

State

Imagine a cube, the lifecycle will be the cube — every time we flip the cube the edges became Events cause they are just an edge in the process, but the State are the lines that connect the cube

States are attached to the stage of activity and very highly bound to the lifecycle of the application. Stages look like an enum of the next variables, DESTROYED, INITIALIZED, CREATED, STARTED, RESUMED, and a function to compute the stage we are in

Events

Events are pretty much like the functions we know already for lifecycle, like ON_CREATE, that are constants used by the LifecycleOwner, whatsoever this enum has 4 important functions that give us the coordinates of the map of our lifecycle, these functions tell us, where we are, where are we gonna be, and where we were

  • downFrom() — returns an event that will be reported to the lifecycle, leaving the specific stage to a lower one
  • downTo() — returns the event that will be reported to the lifecycle that will be entering the lifecycle
  • upFrom() — return an event leaving the specific stage to a higher event
  • upTo() — returns an event entering the specific stage from a lower stage

These functions are the magic behind how LiveData works, but also give us a huge inside of how events get triggered for example ON_PAUSE event will be dispatched before the LifecycleOwner’s related method is called, but this will not apply for ON_START that will be dispatched after the function onStart returns.

LifecycleOwners

An owner is defined as an interface that has an Android lifecycle, for example, an Activity has a lifecycle owner that is the Activity itself, so the activity is in charge of the implementation of this interface — Contrary to the popular belief the Fragments and Activities already are using this interface and they set up the owner when they are created — however, access to the Owner change a little bit depending on the class

// Activity - how to get the lifecycleOwner
this
// Fragment - how to get the lifecycleOwner
viewLifecycleOwner

ViewTreeLifecycleOwner

This class is quite important cause is in charge to set the LifecycleOwner responsible for managing the given View but also of retrieving the LifecycleOwner responsible for it.

Inside Fragment.java — we can find that the ViewThreeLifecycleOwner get called setting up the Owner as the Fragment itself — this also happens in the Activity (in a different function)

void performCreateView(@NonNull LayoutInflater inflater, 
@Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
// lot of code
mViewLifecycleOwner.initialize();
ViewTreeLifecycleOwner.set(mView, mViewLifecycleOwner);
// more code
}

ProcessLifecycleOwner

This class probably is one of the most powerful on Android cause give us the possibility to access the whole application process, when I say whole, I mean that even if you have a multiactivity application or multiple fragments, you will end up with a LifecycleOwner that will contain all your activities, where some events will be dispatched quite different to the one we usually have on an Activity, for example:

  • OnCreate — Dispatched only once, when the app is created
  • OnDestroy — Will be never dispatched
  • OnStart and OnResume — Will be dispatched to the process
  • OnPause and OnStop — Will be dispatched but with a delay, so if the app goes to a rotation momentum, the process will not be created right away

For this part of the serial, we are not gonna touch, the now deprecated @OnLifecycleEvent— but we will discuss it on the process of collection of Flows.

This is all for this part of the series. For the next part, we will discuss Flows and LiveData and we will address the problem that has when consuming them in a View.

If you need help, I’m always happy to help, you can find me here:
Medium as Dinorah Tovar
Twitter as
@ddinorahtovar
StackOverflow as
Dinorah Tovar

--

--

Dinorah Tovar

Google Developer Expert on Android | Doing Kotlin | Making Software 24/7 | Kotlin Multiplatform | She/Her | Opinions are my own, and not my employer