Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save smartherd/dcbfe8c97848f5a6a28a452b8baad932 to your computer and use it in GitHub Desktop.
Save smartherd/dcbfe8c97848f5a6a28a452b8baad932 to your computer and use it in GitHub Desktop.
title: Create Lifecycle-Aware Component
description: ... 

Slide

[Slide 01] $[//] $

So let's explore how we create a lifecycle-aware component.

[Slide 02] $[//] $

So, in this given slide, if you want to implement a lifecycle-aware component then you basically need two things.

[Slide 03] $[//] $

First you need to find a class which implements LifecycleOwner interface.

Luckily, both Activity and Fragment by default implement this interface. So you can also assume that any class which implements this interface is basically known as 'lifecycle owner'. Please, remember this.

Second thing that you would need is a class

[Slide 04] $[//] $

which implements the DefaultLifecycleObserver interface. Well, the task of this interface is to observe the lifecycle events taking place in the lifecycle owner class which is either Activity or Fragment in our case.

The third step is of subscription. So, we need to make this class subscribe to the lifecycle owner. Fine?

So let's see some code in action.

Demo

[FADE Android Studio] $[//] $

Now, before we start go to module level build.gradle file. Here, at the bottom you will find all the dependencies which will help us to use lifecycle related classes in our project. So these three are our tutorial dependencies.

[Official Website] $[//] $

If you go to the Official documentation of androidx.lifecycle, here, you will find a lot of more specific dependencies which you might require in your project depending on your use case. I have already added important ones in our project. You should at least scroll down through this documentation if you want in-depth knowledge of any specific topic.

Anyways, let's get back to our studio.

[FADE Studio] $[//] $

Ok, let's open our MainActivity.

Now, if you go deeper into the base classes of this MainActivity such as open this AppCompatActivity. Again open this super class FragmentActivity. Open this ComponentActivity. Here, you'll find this ComponentActivity implements the LifecycleOwner interface. So this makes our MainActivity as the LifecycleOwner class i.e. it has its own lifecycle. So let's close all the files. Perfect!

So we don't have to implement any more interface in our MainActivity.

Now, in our MainActivity we have a lot of code. But, one thing that gains my attention is the code related to NetworkMonitor i.e. within our onCreate function we are initializing our NetworkMonitor. We are calling its init() method. Then within onStart we are registering the network callback.

And in onStop we are unregistering the network callback.

[OPEN FILE] $[//] $

Now, if you take a look at this class NetworkMonitor, which is present over here, then you will find that this class is basically responsible to manage the internet connection.

In short, this class has nothing to do with managing the user interface. It only monitors the network connection.

So in our MainActivity, all the code related to this class NetworkMonitor should be moved out. Because as per the clean architecture the MainActivity should only contain code related to managing Views. Right?

So to make this happen, we need to first convert this NetworkMonitor into a lifecycle-aware component.

And we can do that by just implementing DefaultLifecycleObserver interface.

Because the code related to NetworkMonitor is dependent on the lifecycle of the Activity.

So we can make this NetworkMonitor a lifecycle-aware component. By just implementing a interface of DefaultLifecycleObserver. And that's it, we're done. This class is now a lifecycle-aware component.

But our last step will be to connect this NetworkMonitor with this MainActivity.

Well, we can do that within onCreate function, right after this initialization by using getLifecycle() method dot addObserver() method and pass the instance of NetworkMonitor. Like this!

lifecycle.addObserver(networkMonitor)

So this statement, creates a connection between our lifecycle observer with the lifecycle owner.

Back to Slide

[Slide 05] $[//] $

So, to summarize the steps involved, the first step is to identify the lifecycle owner, which in our case was the MainActivity. The second step is to identify the lifecycle observer which will be our lifecycle-aware component.

And the third step will be to connect the lifecycle observer with the lifecycle owner. So yes, that's all for this episode.

In the next episode, we'll move out all the code related to NetworkMonitor from the MainActivity.

So that's all for this episode, in the next episode, we will move all code related to NetworkMonitor from this MainActivity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment