Now, among Activity, Fragment and ProcessLifecycleOwner class. What’s common in them?
Well, they all by default implements the LifecycleOwner interface.
Which makes them dispatch lifecycle events to all its observers. So, until now, we know this much.
Now, can we create our own custom LifecycleOwner class?
Well, of course yes. You can convert any normal class into a LifecycleOwner class which can dispatch lifecycle events just like an Activity.
So let’s see how to do it.
In our MainActivity, if you scroll down at the bottom, we have a class NetworkObserver.
It implements DefaultLifecycleObserver, which means this class is a lifecycle-aware component.
This class overrides onStart and onStop method. This means this class is ready to respond to ON_START and ON_STOP events of its lifecycle owner. And based on those events it will execute these two methods which are defined over here.
Now, the functionality of these two methods is to just show and remove the SnackBar from the screen using these two functions respectively.
Again, nothing too complex here as well.
So at the moment, our lifecycle-aware component is ready to use.
Now, our next step will be to identify it’s LifecycleOwner class. And for that, let’s open our UnavailableConnectionLifecycleOwner class within our monitor package.
For simplicity I have written steps, how to convert this class into a custom LifecycleOwner.
So the first step is to implement the LifecycleOwner interface.
Let’s do it.
The second step is to initialize LifecycleRegistry.
Let’s do that as well.
val lifecycleRegistry: LifecycleRegistry = LifecycleRegistry(this)
The primary purpose of this LifecycleRegistry is to emit lifecycle events and notify all observers. We’ll check it out shortly.
Next, step number 3 is to override getLifecycle() method and return lifecycleRegistry instance which we just created.
Let’s do that as well.
override fun getLifecycle(): Lifecycle {
return lifecycleRegistry
}
Great!
Now, our last step is to emit lifecycle events and notify observers. And for that we will make use this lifecycleRegistry instance.
So first let’s create two functions onConnectionAvailable and onConnectionLost.
fun onConnectionAvailable() {
}
fun onConnectionLost() {
}
Now, when the network connection is available, we will emit ON_START event.
lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START)
So this function handleLifecycleEvent will emit this ON_START event and any observer listening to this event can catch it and react to this lifecycle change. Fine?
Similarly, when the network connection is lost, we will emit ON_STOP event.
lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_STOP)
Now, I will copy this from here, paste it here and change it to ON_STOP.
Now, it’s totally upto you which event you want to emit. Right now, we are emitting ON_START and ON_STOP event. So accordingly in our observer class which is our inner class over here, we have overridden onStart and onStop methods to respond to these ON_START and ON_STOP events being emitted from here.
I guess things are clear now. In the next episode, we’ll make the observer class subscribe to this custom lifecycle owner class.