Your next career begins with
Save 50% off your seat in our next iOS Bootcamp. Limited time only. Sessions start April 3.
Lifecycle-Aware Components in Android
Nov 1 2022 Kotlin 1.6, Android 12, Android Studio Chipmunk 2021.2.1
Part 1: Lifecycle-Aware Components in Android
2. Understand Lifecycle-Aware Components

Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
About this episode
Explore what a lifecycle-aware component is, and understand what problem it solves in your app.
Instructors
Contributors
Over 300 content creators. Join our team.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Watch this episode for free — or access the full course as a Kodeco Subscriber. Learn more about the benefits of subscribing here
This video Understand Lifecycle-Aware Components was last updated on Nov 1 2022
So let’s start exploring concepts with a very simple question. What exactly is meant by a lifecycle-aware component?
And, what problem does it solve in our application? Why should we use it?
Well, we all know what exactly is a Activity
and a Fragment
. We also know - they both contain lifecycle methods which are used to perform certain actions based on their states.
For example, in Activity’s onStart method, we can register a few components and use those components when the Activity
is in running state. At the same time, we also need to unregister those components in Activity’s onStop
method.
This sounds really simple, isn’t it? But in production level apps where you could have multiple components to register and unregister, it is quite possible you forget to unregister a few components within this onStop
method. Which could obviously lead to bugs, memory leaks, and even make your application to crash.
Well, the same problem is applicable for fragments as well.
Moreover, adding too much code within these lifecycle methods could make your code messy, error-prone, and difficult to maintain in the long run.
In fact, we should only keep UI related code within our Activity
or Fragment
. Any other code other than that of UI should be kept out of these two classes.
So what is the solution for this? How can we ensure our code is organised and maintenable at the same time? So here comes lifecycle-aware components to the rescue.
To help us keep our code organised, we can create a separate class known as lifecycle-aware component. Well, this lifecycle-aware component is nothing but a simple class.
The role of this class is to react to the lifecycle changes taking place within this Activity and accordingly execute the code. So basically we can extract lifecycle related code from these methods and put them in this class and therefore, keep our Activity code clean and simple.
And since, this component is aware of the lifecycle of Activity in this case, that’s the reason it is known as ‘lifecycle-aware component’.
And for this Fragment as well, we can create its own Lifecycle-aware component, extract the code from these lifecycle-methods and put them within this component, which will of course keep the code well organised.
Okay, so now that we know what is a lifecycle-aware component, let’s start creating our very first lifecycle-aware component in the next episode.
All videos. All books.
One low price.
A Kodeco subscription is the best way to learn and master mobile development — plans start at just $19.99/month! Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.
Learn more