Use of Lifecycle Owner and Observer in Android Jetpack Library

Dr. Vipin Kumar
3 min readSep 6, 2021

Lifecycle-aware components perform actions in response to a change in the lifecycle status of activities and fragments. These Lifecycle-aware components help us to produce better-organized, and often lighter-weight code, that is easier to maintain.

A common pattern that we use to add our all codes in the methods of lifecycle of activities and fragments like in onCreate(), onResume() and etc.. However, this older pattern leads to a poor organization of the code and to the proliferation of errors. By using new Jetpack library lifecycle-aware components, you can move the code of dependent components out of the lifecycle methods and into the components themselves.

In Android, 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.

Lifecycle aware components are divide into 2 parts:

1. Lifecycle Owner: Lifecycle owner are the activity or fragment that have their own lifecycle and events that will fire on the basis of their lifecycle.

2. Lifecycle Observer: Lifecycle owner are the model or logic code that we want to execute on the basis of activity or fragment lifecycle, but also want to keep it separate from activity or fragment lifecycle methods.

It is very much clear from diagram below, observers constantly observe owners like activity or fragment lifecycle and owners executes observer methods as per the binding done by the coders.

Figure 1:Lifecycle Observer and Owner

Let’s us take an example to understand Lifecycle Observer and Owner:

Step 1: Firstly, we need to add lifecycle dependency in module level build.gradle file, as given below:

var lifecycle_version = “2.4.0-alpha03”
implementation(“androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version”)

Step 2: In step 2, we need to create an observer class that will link with owner or activity, this class must be sub class of LifecycleObserver class. In this example, we are taking name of class is Observer, you can take any name of your choice, but must be sub class of LifecycleObserver as given in example below; In this class, we have created one methods called onCreate(), we may take any name, but I want to link it with onCreate() method of Activity, that’s why I have taken it name onCreate(). I have created only one method, but you can create any number of methods and can be link with any number of methods of activity like onResume(), onPuause() and etc.

Now we need to link it with onCreate() method of Activity, so we have to insert annotation @OnLifecycleEvent(Lifecycle.Event.ON_CREATE). If you want to link it with other methods than you can choose as per your choice like Lifecycle.Event.ON_PAUSE for onPause() of Activity.

class Observer(var context:Context):LifecycleObserver {
@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
fun onCreate()
{
Toast.makeText(context.applicationContext,”Link with Activity onCreate() Function”,Toast.LENGTH_SHORT).show()
}
}

Step 3: At the end, we need to add this observer to the activity, here we have only one activity MainActivity. So I have to add this observer class in MainActivity as observer. We can add this by using lifecycle.addObserver() method. See the example below to understand it.

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
lifecycle.addObserver(Observer(this))
Toast.makeText(applicationContext,”On Create in Activity”,Toast.LENGTH_SHORT).show()
}
}

Output of this App is:

Execution of onCreate in MainActivity
Execution of onCreate in Observer Class

--

--

Dr. Vipin Kumar

Assoc. Prof. , DCA & Assoc. Head (SD), SDFS, at KIET, PhD (CS) My YouTube Channel: Dr. Vipin Classes (https://www.youtube.com/channel/UC-77P2ONqHrW7h5r6MAqgSQ)