Okay, so we have created our LifecycleOwner.
We have our observer class in place.
Now, let’s connect these two classes using addObserver method.
We can do that, within our MainActivity itself.
So at the top, you will find. We have already initialized UnavailableConnectionLifecycleOwner class using the dependency injection.
And also we have initialized NetworkObserver class.
So it’s time to connect them. Let’s do that within our onCreate function over here.
let’s use unavailableConnectionLifecycleOwner class .lifecycleRegistry and use addObserver method and pass the instance of networkObserver. Great!
unavailableConnectionLifecycleOwner.lifecycleRegistry.addObserver(networkObserver)
And also as a part of good programming practise, if you don’t want to expose this lifecycleRegistry instance to this MainActivity then for that what we can do is inside this owner class we can change from public to private and also we can create a function over here such as addObserver method and import this LifecycleObserver package.
fun addObserver(lifecycleObserver: LifecycleObserver) {
lifecycleRegistry.addObserver(lifecycleObserver)
}
And within this function we can use lifecycleRegistry.addObserver method and pass this lifecycleObserver reference.
And then from our MainActivity, we can remove this lifecycleRegistry and directly use addObserver method.
Perfect! It feels great to use abstraction sometimes.
Now at the moment, in our application, if you go to lifecycle owner class, we are not executing these two functions from anywhere in our application.
So we need to execute these two functions, so that events are emitted.
So in our MainActivity, if you scroll down at the bottom.
You will find handleNetworkState function. Righit now, the puspose of this function is to show a SnackBar when the network connection is unavailable.
So instead of directly calling this function, we can replace it by unavailableConnectionLifecycleOwner.onConnectionLost() function.
Add similarly, let us define one more case of
when the network becomes available call onConnectionAvailable function.
NetworkState.Available -> unavailableConnectionLifecycleOwner.onConnectionAvailable()
Okay, this might be a little confusing. So let me walk you through the entire flow again. When the network is gone or becomes available then these two functions will be executed. So this is going to execute the two function within our lifecycle owner class, which are defined over here.
And these two functions will trigger an event ON_START and ON_STOP. Right?
And these two events will be catched by our NetworkObserver class which is our lifecycle-aware component.
So those two events will be catched by these two functions respectively. And therefore, they will execute, these two functions to show and hide the SnackBar from screen.
So I am sure things are pretty clear now.
Let’s run the application.
So right now we have active internet connection. So what I will do is I will just turn-off the internet.
So here we go, we got a SnackBar on screen ‘Network connection is gone! RETRY’ which is due to the execution of this method over here. Now once we turn on the internet then it should remove the SnackBar from screen.
So let’s see if that’s working or not?
Ok, our SnackBar is gone. Just because this event of ON_START was dispatched which was catched by this function over here.
And this in turn removed the SnackBar alert from the screen. Perfect, isn’t it?
So we have successfully created a custom lifecycle owner and even emitted lifecycle events. So yes, that’s all for this episode.