Android Architecture Components: Lifecycle’s

Imran Bilal Solanki
AndroidPub
3 min readMar 26, 2019

--

This is my series of blogs on Android Architecture Components.

This blog is about ‘Life cycle aware components’.

Content

  1. What is android architecture components?
  2. Meaning of the term ‘life cycle aware’?
  3. How to make a component life cycle aware?
  4. Advantages

What is android architecture components?

Developing android application in most of the cases is content-based UI development.

As a mobile app developer one knows the pain of maintaining an app in a fast-paced delivery environment.

Android architecture components are a set of libraries which allows you to architect robust, production quality app which is testable and easy to maintain. They help overcome boilerplate tasks. In nutshell, they are also the solution to many challenges that a developer have been facing until now. One of which is the topic of this blog- Managing Life cycle.

The other components of AAC include- Data Binding, View Model, Live Data, Room etc.

Meaning of the term ‘Lifecycle aware’ ?

The developer designs screens using different constructs provided by the Android SDK. For eg: Activity and Fragments. As a developer, one knows that the screen can be in either of a predefined state or lets say callbacks — onCreate(), onStart(), onResume(), onPause(), onStop(), onDestroy() etc.

As the development of screen or a component continues, the code within these callbacks increases. Many of the dependent module or code needs to be taken care of, within the lifecycle. This induces a tight coupling!

In many cases, the obvious reason for the component to become dependent is because they are unaware of Activity/Fragment’s lifecycle. To simplify let us consider below code of an Activity

In the above code segment, the camera component is part of the lifecycle of the activity ! Hence in each of the state, the functionality of a camera is addressed carefully. Now as the feature grows, code within these individual callback increases and hence the readability decreases and subsequently the maintenance becomes tough.

One can not segregate the camera code independent of Activity/Fragment(screen).

How to make a component life cycle aware ?

The solution to the above problem is making camera component independent and aware of CameraActivitie’s lifecycle. It is achieved in two steps

  • Create a class and implement interface LifecycleObserver. Now a developer can consume the events of interest like ON_CREATE, ON_START etc.

For simplicity, only a few of the events are handled. The events are consumed using annotation and hence the developer is free to choose any function name.

  • Step 2- Associate the component with the activity OR fragment.

Now, as and when there is a change in the life cycle of activity/fragment, then the component receives the EVENT and respective callback gets called.

Advantages

Making a dependent component life cycle aware, benefits app in the following ways

  1. As seen clearly the code of activity class has decreased drastically and is simple to read.
  2. The CameraComponent class has become independent of activity/fragment.
  3. Reusability: As the CameraComponent is Independent and Lifecycle-Aware, it is ready to be reused without code duplication in activity/fragments.

Now in my next blog, I will cover the other architecture component — Live Data

--

--