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

07. Use ProcessLifecycleOwner

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: 06. Use Lifecycle States Next episode: 08. Create Custom Lifecycle Owner

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: 07. Use ProcessLifecycleOwner

Okay, so far we have seen Lifecycle states and events of Activity and Fragment. Now, the question arises is there any way to respond to lifecycle changes of the entire application?

The answer is yes, but we need to use a special class of ProcessLifecycleOwner. Because this class implements the LifecycleOwner interface and this class is responsible to provide lifecycle for the whole entire application process.

Let’s take a look at this class.

  • This class implements LifecycleOwner interface
  • It dispatches ON_CREATE event only once when the application starts but also note that it never dispatches ON_DESTROY event even when the application is entirely destroyed.
  • It dispatches ON_START and ON_RESUME events when the first Activity in app goes through these states.
  • Similarly, it dispatches ON_PAUSE and ON_STOP events when the last visible Activity of the app goes through these states.
  • Now, when should we actually use this class?

Well, suppose when the user sends your application to the background by minimizing the application or he brings it back to foreground then in that case we can react to lifecycle changes of the application and execute code accordingly as required.

So, we are going to follow the same three steps which we used earlier. But this time our LifecycleOwner class would be in our RecipesApplication.

The lifecycle observer i.e. our lifecycle-aware component will be AppGlobalEvents.
And, we will use addObserver() method to subscribe to the lifecycle events of the application. Okay, enough of talking now. Let’s see some code in action.

Let’s open our AppGlobalEvents class. Here, you will find we already have code in place. Such as we have implemented the DefaultLifecycleObserver interface.

Overridden onStart and onStop methods. So this onStart method will be executed when the first Activity of this application goes through ON_START event. And this onStop will be executed when the last visible Activity on screen goes through ON_STOP event. Fine?

And accordingly we are calling these two methods, within these two functions which are defined over here. Well, these two functions are just going to print this message in LogCat and implementation of this function is actually present inside `AnalyticsEventsImpl’ class, over here. So nothing too complex until now.

Let’s close this file. And now, let’s open our RecipesApplication which is our Application class.

Here, we will get the instance of AppGlobalEvents which is this file over here using the dependency injection. And within onCreate function, let us write some code to react to the lifecycle events of the application.

And for that, we’ll make use of the ProcessLifecycleOwner class. Use .get() function. This will return the LifecycleOwner instanc. Then use .lifecycle property and then call addObserver() method and pass the instance of appGlobalEvents. And that is it we’re done.

    ProcessLifecycleOwner.get().lifecycle.addObserver(appGlobalEvents)

So this statement will subscribe AppGlobalEvents to the lifecycle changes of entire application. So let’s run the application.

So in our LogCat I am using the filter of APP_LOGGER. You can use the same LogCat filter. Now, as soon as the application came to the foreground for the first time, we got printed ‘App is going to foreground’ because of the execution of ON_START method.

Now, let’s minimise the application. So as soon as we minimized the application, the application went to the background. In that case, we got ‘App is going to background’ message.

Now, let’s again bring our application back to foreground. So here we go, we got this message ‘App is going to foreground’. So yes our application is working as per our expectation.

Well, right now I have just used a basic code of LogCat within these two functions. In your production application, you can register and unregister application level components within these two lifecycle functions. Fine? So yes, that’s all for this episode.