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

04. React to Lifecycle Events

Episode complete

Play next episode

Next
About this episode
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 03. Create Lifecycle-Aware Component Next episode: 05. Explore Lifecycle Events Emitted by the LifecycleOwner

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Transcript: 04. React to Lifecycle Events

Okay, within our MainActivity within our onCreate method we have a code networkMonitor.init(). So what we can do is within our NetworkMonitor, since we have already implemented this DefaultLifecycleObserver. Right here we can override the onCreate() method. Like this! And this method basically belongs to this interface of DefaultLifecycleObserver. So here, we can call the init() method. Like this!

And remember this onCreate() function will be executed right after the execution of onCreate() method present inside the MainActivity over here.

So now, we can safely remove this code from here. Similarly, within our onStart and onStop method we can remove the code from here and put it inside the NetworkMonitor class, like we did before. Now, at the moment, you might think - hey this is just one line of code. Why shouldn’t we keep it here itself? Why take so much pain to shift it outside the MainActivity.

Well, right now our app is very-very simple. Imagine an application that could have so many activities and a lot of fragments and each of them has a lot of components to register and unregister. Sp in that case, your onStart and onStop method would be bloated with a lot of boiler-plate code. Which is not good, right? So let’s shift this code as well.

So within our NetworkMonitor right after onCreate we can override onStart method and call registerNetworkCallback() function.

We can override onStop method. and here as well we can execute unregisterNetworkCallback(). Similarly, within onStop method after overriding it we can unregister the network callback. Great!

And here, I guess we have some warning - we can convert this to private. And similarly, we can convert these two functions as private as well. Because we will remove code from the MainActivity. So from the MainActivity we can entirely remove these lines of code. Perfect!

You can now run the application. And your application will work as it was working before. Great, isn’t it? So now, let’s explore more.

If you take a look at the interface of this DefaultLifecycleObserver. Then you will find all the lifecycle methods that are typically present in an Activity or a Fragment. Such as onCreate through onDestroy. You can override each of them as per your application requirement.

Also, if you take a closer look at these methods, you’ll find these methods are associated with a lifecycle event. Such as onStart method is associated with ON_START event. onResume method is associated with ON_RESUME event.

Well, this actually means whenever the onStart method of our Activity or Fragment is executed, then an event is triggered - which is this ON_START event. And it is due to this event, the onStart method of the observer which in our case is NetworkMonitor is executed.

So internally this is how our framework is executing methods and triggering lifecycle events. So in the next episode we’ll talk more about these events and understand them better.