What are Lifecycle-Aware Components in Android?🚀

Debanshu Datta
Backyard Programmers
5 min readJan 1, 2021

--

Photo by Sara Riaño on Unsplash

Hey everyone, I will start a blogging series on Android Jetpack Components trying to document my progress and also deliver a walkthrough with hands-on projects alongside. 🥇

In series 1 we will focus on the architecture components of Jetpack libraries. Let me start this series with the biggest question.

Why Android Jetpack?

In simpler terms, starting with android development, we don’t actually care much about the architecture or design pattern of the code, but as the size of the app grows; it becomes really challenging to maintain and add features.

To solve this Android teams came up with a set of components and design guidelines that will not only reduces the boilerplate code, make the developers easy to operate and test code besides helping new developers to get the most out of android and bringing all under the same canopy.

According to the official Android developer site,🎮

Jetpack is a suite of libraries to help developers follow best practices, reduce boilerplate code, and write code that works consistently across Android versions and devices so that developers can focus on the code they care about.

The components are divided into 4 parts:

  • Architecture
  • UI
  • Foundation
  • Behaviour

All the components are both independent and compatible with each other. Jetpack encompasses a collection of Android libraries that incorporate best practices and provide backwards compatibility in your Android apps.

Let’s get started🤖

Today’s Topic: Lifecycle🚀

What is the Activity/Fragment Lifecycle?

Activity & Fragment Lifecycle

First, clear your concepts regarding the Activity and Fragment lifecycle, functions, and events related to it. Refer to the documentation it the best resource for an adequate synopsis. ✨

What are Lifecycle-Aware Components?

We have to sometimes implement actions to a dependent component according to the lifecycle of an activity/fragment. It is a typical method to implement these actions on the lifecycle methods is by overriding them, but this a counterproductive practice and leads to many troubles when the size of the code base increases.

Therefore, Lifecycle-aware components perform actions in response to a change in the lifecycle status of another component, such as activities and fragments. These components help you produce better-organised, and often lighter-weight code, that is easier to maintain.

The androidx.lifecycle package provides classes and interfaces that let you build lifecycle-aware components—which are components that can automatically adjust their behaviour based on the current lifecycle state of an activity or fragment. The androidx.lifecycle package provides classes and interfaces that help you tackle these problems in a resilient and isolated way.

For easier understanding, I will only cover the basic concept which will be enough for now. Theandroidx.lifecycle package has many classes the most significant are there:

Simple Diagramatic Image

— Lifecycle

In simple terms, it defines an object that has an android lifecycle attached to it. The object basically retrieves information about the current state LifecycleOwner, like activities and fragments. Lifecycle uses two main enumerations to track the lifecycle status for its associated component: Events (Ex- onCreate())and States (Ex- CREATED).

States and events that comprise the Android activity lifecycle

— LifecycleOwner

Classes that implement LifecycleOwner’s single method [ie, getLifecycle()] interface that denotes that it has an android lifecycle. For simpler understanding, think of it as an activity or a fragment.

Components that implement LifecycleObserver work seamlessly with components that implement LifecycleOwner because an owner can provide a lifecycle, which an observer can register to watch.

— LifecycleObserver

It basically observes the LifecycleOwner and keeps track of its Lifecycle. Thus the Observer performs actions. The action performed by the observer depends on the Lifecycle of the LifecycleOwner.

Single line Summery for all the above🥳

Every LifecycleOwner has Lifecycle, based on EVENT or STATE of this Lifecycle of LifecycleOwner, LifecycleObserver performs the action.

Hands-On Project🎯

We will do a basic project in which we will attach an Observer to the MainActivity of a simple Android project.

Create a simple android project with basic activity, nothing special. We will use Kotlin.

Dependencies-

Not required previously is was required but present it works perfectly fine with no additional dependencies.

Code-

  • Make a new file of the name MainActivityObserver.kt and extend the LifecycleObserveinterface. LifecycleObserver uses the annotation @OnLifecycleEvent() to keep track of the event which takes the parameter of Event which comes from the lifecycle class, for example, Lifecycle.Event.ON_CREATE. Then create a function example added a simple Log statement.
fun onCreateEvent(){
Log.i(TAG,"Observer ON_CREATE")
}
  • Now let's change the MainActivity.kt. Inside the onCreate() method, add these two lines.
Log.i(TAG,"Owner ON_CREATE")
lifecycle.addObserver(MainActivityObserver())
Code will look like this

When you run the code:

You can see the Logcat(First the Owner is run then the Observer)

Now let's override all the functions, onCreate(),onStart(),…onDestroy(), etc. See the gist given below follow the steps as above.

The output you will get,

Upon running the app you see that from onCreate() to onResume() runs with LifecycleOwner first followed by the LifecycleObserver

When you will rotate the device you find that from onPause() to onDestroy() the LifecycleObserver is called first then the LifecycleOwner. You must be familiar with the basic lifecycle of activities to get a proper understanding.

References:

After this, if you have time to go through them for a better understanding

For any doubts and suggestions, you can reach out on my Instagram, LinkedIn. I would be more than happy to give a helping hand.

I will well appreciate one of these 👏

--

--

Debanshu Datta
Backyard Programmers

Android @Gojek | Mobile Developer | Backend Developer (Java/Kotlin)