Jetpack — Life Cycle

Himanshu Srivastava
Nov 1 · 3 min read

Life cycle is a term, every one does know about. Life cycle is associated to every thing in this world, whether it is living, non living even in computer programming also, life cycle is associated to every variable, function, program etc.

Like Every other thing, components like Activity, Fragments do have Life cycle associated with them.

Life cycle of an Activity

In conventional way, we use these life cycle call backs to perform specific actions when activity state changes. But this can make code maintenance harder.

So, Android came with a new idea to manage these Life cycle aware components. They came up with Life cycle framework in jetpack. And this really removed the overhead of overloading our Activity/Fragment with a lot of code.

To use Jetpack — LifeCycle in Android. Put this line inside dependencies in build.gradle file

def lifecycle_version = "2.1.0"
implementation "androidx.lifecycle:lifecycle-runtime:$lifecycle_version"

There are three things.

1 . Life Cycle — Life Cycle is class which holds the information about the Life cycle of the Associated component. It maintains two enumerations Event and State

Event : Life cycle events are dispatched from Framework and Life cycle class. These events are mapped to callback events of the Associated components.

State : This enumeration keeps the track of current state of the associated component.

2 . Life Cycle Owner — This is an interface, it has a single method. getLifeCycle(). This methods must be implemented by the class implementing this interface. If we want to manage the life cycle of the entire application then we have to use ProcessLifeCycleOwner.

3 . Life Cycle Observer — This class work seamlessly with Lifecycle Owner. this class can contain code for the every different states of the associated component that we have written in Life cycle callbacks within our Activity/Fragment.

class MyLifecycleObserver : LifecycleObserver{

private val TAG = "MyLifecycleObserver"

@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
fun created(){
Log.d(TAG, "Created called")
}


@OnLifecycleEvent(Lifecycle.Event.ON_START)
fun started(){
Log.d(TAG, "Started called")
}

@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
fun resumed(){
Log.d(TAG, "Resume Called")
}

@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
fun paused(){
Log.d(TAG, "Pause called")
}

@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
fun stopped(){
Log.d(TAG, "Stop Called")
}

@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
fun destroyed(){
Log.d(TAG, "Destroy Called")
}
}

To add this observer in Lifecycle(Can be directly accessed in AppCompatActivity) add these lines in onCreate() of Activity class. But this works only if You want to use Default Life cycle owner.

lifeCycleObserver = MyLifecycleObserver()
lifecycle.addObserver(lifeCycleObserver)

If you want to go for custom Life cycle owner then you have to use LifecycleRegistry and if Activity state changes you have to mark state as current state using lifeCycleRegistry.markState().

class MyLifeCycleOwner : LifecycleOwner{

private val TAG = "MyLifeCycleOwner"

private lateinit var lifeCycleRegistry : LifecycleRegistry

constructor() {
this.lifeCycleRegistry = LifecycleRegistry(this)
}

override fun getLifecycle(): Lifecycle {
return lifeCycleRegistry
}

fun markState(state : Lifecycle.State){
lifeCycleRegistry.markState(state)
}

}

and Activity will look like below

class MainActivity : AppCompatActivity() {

private lateinit var lifeCycleObserver : MyLifecycleObserver
private lateinit var lifeCycleOwner: MyLifeCycleOwner

override fun onCreate(savedInstanceState: Bundle?) {

super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

lifeCycleObserver = MyLifecycleObserver()
lifeCycleOwner = MyLifeCycleOwner()
lifeCycleOwner.markState(Lifecycle.State.CREATED)
lifeCycleOwner.lifecycle.addObserver(lifeCycleObserver)

}

override fun onRestart() {
super.onRestart()
lifeCycleOwner.markState(Lifecycle.State.STARTED)
}


override fun onResume() {
super.onResume()
lifeCycleOwner.markState(Lifecycle.State.RESUMED)
}

}

You can refer this sample project on git hub.

Himanshu Srivastava

Written by

I have been developing Mobile Applications since 2015. I try to learn something new every day and I hope my learnings can help you.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade