DEV Community

Cover image for My app is on background or foreground?
Dinorah Tovar
Dinorah Tovar

Posted on

My app is on background or foreground?

Solving the problem for OnPause & OnResume using LifecycleObserver

One of the most asked questions on StackOverflow is something like “How do I know if my app is on background” and we have multiple duplicates like this one or this another one.
This question has been hunting a lot of android developers from a long time, for some time the Android Developers has been doing this:

And a lot of persons are still using a variable that changes depending on the configuration, in my personal opinion, this is not the right approach to solve this issue, let me explain why, on our last project we were using multiple fragments and activities with different flows and navigations, but every time the users goes to another app, maybe open the task manager or the phone gets locked, we need to show the user a “Pin View” it means you are locked from your session till you verify your pin and do a login.

The problem

We have multiple views and multiple fragments and using MyApplication to solve this problem sounds like tons and tons of work.
We thought, maybe if we do a BaseAppCompatActivity we can solve this issue of the multiple OnPause and OnResume, but this was, again, not the right solution.
When you change from one activity to another activity, the Activity A goes to OnPause for a second and we were able to see the Pin View.

The Solution

The final solution was based on LifecycleObserver, which is part of the LifeCycle Aware Components proposed on Android Jetpack, so we made an ApplicationOwner class that extends from LifecycleObserver and use OnLifecycleEvent to check the “events” inside an Application.

We decide to use Events to check when the app was starting or getting stopped. That helps us a lot, cause the lifecycle events are dispatched from the framework and the Lifecycle class. These events map to the callback events in activities and fragments.
But maybe you are thinking, this is the same problem you have with going from Activity A to Activity B, you are going to see the Pin View when you changed it, well, not exactly, cause we decide to use ProcessLifecycleOwner.

This class provides lifecycle for the whole application process, and you can implement it like this:

You can consider this LifecycleOwner as the composite of all of your Activities, except that ON_CREATE will be dispatched once and ON_DESTROY will never be dispatched. Other lifecycle events will be dispatched with the following rules: ProcessLifecycleOwner will dispatch ON_START, ON_RESUME events, as a first activity moves through these events. ON_PAUSE, ON_STOP, events will be dispatched with a delay after a last activity passed through them. This delay is long enough to guarantee that ProcessLifecycleOwner won't send any events if activities are destroyed and recreated due to a configuration change.

We put this on MyApplication to catch only the application and not all activities.

For more information please visit: (Handling Lifecycles with Lifecycle-Aware Components)[https://developer.android.com/topic/libraries/architecture/lifecycle]

Happy Coding! 👩🏻‍💻
If you have questions, you can reach me here at Dev Community or:
Medium: https://medium.com/knowing-android
Twitter: https://twitter.com/DDinorahtovar

Top comments (0)