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:
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?
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.
androidx.lifecyclepackage 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.lifecyclepackage 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. The
androidx.lifecycle package has many classes the most significant are there:
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-
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
LifecycleObserverwork seamlessly with components that implement LifecycleOwner because an owner can provide a lifecycle, which an observer can register to watch.
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.
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.
Not required previously is was required but present it works perfectly fine with no additional dependencies.
- Make a new file of the name
MainActivityObserver.ktand 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.
- Now let's change the
MainActivity.kt. Inside the
onCreate()method, add these two lines.
When you run the code:
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
onResume() runs with LifecycleOwner first followed by the LifecycleObserver
When you will rotate the device you find that from
onDestroy() the LifecycleObserver is called first then the LifecycleOwner. You must be familiar with the basic lifecycle of activities to get a proper understanding.
After this, if you have time to go through them for a better understanding
- Take this Colab: https://codelabs.developers.google.com/codelabs/android-lifecycles
- Read the Docs really helpful: https://developer.android.com/topic/libraries/architecture/lifecycle.html
I will well appreciate one of these 👏