Lifecycle

As per the documentation, Lifecycle is an abstract class which has an Android Lifecycle attached to it —and, because it is holding this state of the lifecycle, objects can observe this state and react accordingly. In order to keep a track of this state, there are two fundamental concepts (represented as Enums) that the Lifecycle class incorporates:

Event

An event is the lifecycle event that has been trigged by a lifecycle change (such as resuming an activity). In our classes we can implement callbacks for each of these events so that we can handle lifecycle changes. The arch.lifecycle package provides an Annotation for us to use which means we can annotate the methods within our class which should be triggered upon certain lifecycle events.

Without going into implementation detail just yet, this will look like so:

@OnLifecycleEvent(ON_STOP)

We can also handle multiple lifecycle events within the same annotated method, this allows us to trigger a specific method based off multiple lifecycle events being triggered:

@OnLifecycleEvent({ON_STOP, ON_START})

This approach is handy as it means adopting this into your application allows you to annotate the methods that you may already be using for handling lifecycle events, meaning we don’t have to re-write much of our implementation if we wish to incorporate the arch.lifecycle components into our app.

There is an event for several different lifecycle events that we can listen for, the ones triggered will be one of:

ON_ANY — This event will be triggered upon any lifecycle event. This allows you to react upon any change in the lifecycle of what is being observed.

— This event will be triggered upon any lifecycle event. This allows you to react upon any change in the lifecycle of what is being observed. ON_CREATE — This event will be triggered upon creation of the LifecycleOwner.

— This event will be triggered upon creation of the LifecycleOwner. ON_DESTROY — This event will be triggered upon the LifecycleOwner being destroyed.

— This event will be triggered upon the LifecycleOwner being destroyed. ON_PAUSE — This event will be triggered upon the LifecycleOwner being paused.

— This event will be triggered upon the LifecycleOwner being paused. ON_RESUME — This event will be triggered upon the LifecycleOwner being resumed.

— This event will be triggered upon the LifecycleOwner being resumed. ON_START — This event will be triggered upon the LifecycleOwner being started.

— This event will be triggered upon the LifecycleOwner being started. ON_STOP — This event will be triggered upon the LifecycleOwner being stopped.

As you can see, it doesn’t give us access to every single lifecycle event that may be triggered within an activity or fragment — but it’s enough to give us the ability to manage the lifecycle of views and other components with ease and efficiency.

Creational lifecycle events (ON_CREATE, ON_RESUME, ON_START) will all be called after the LifeCycleOwner corresponding method has returned. Whilst teardown lifecycle events (ON_DESTROY, ON_PAUSE, ON_STOP) will be call before the LifeCycleOwner corresponding method has been called. This is so that we can handle these lifecycle events once our activity/fragment has handled any setup and also before it is destroyed.

State

The state of a Lifecycle is essentially the point is is at between two lifecycle events. After an event is triggered the Lifecycle will enter one state, then leaving it and entered another state when another event is triggered.