Foreground or Background?

Dinorah Tovar
Knowing Android
Published in
3 min readNov 15, 2018

Solving the problem for OnPause & OnResume using LifecycleObserver

Resources available on Sketch Resources

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 other one.
This question has been hunting a lot of developers from a long time, for some time the Android Developers has been doing this:

@Override
protected void onResume() {
super.onResume();
MyApplication.activityResumed();
}

@Override
protected void onPause() {
super.onPause();
MyApplication.activityPaused();
}

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 implemented it like this:

ProcessLifecycleOwner.get().lifecycle.addObserver(ApplicationOwner())

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 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

If you need help, I'm always happy to help, you can found me here:

Medium: https://medium.com/@dinorahto
StackoverFlow: https://stackoverflow.com/users/4613259/dinorah-tovar

Happy Coding! 👩🏻‍💻

--

--

Dinorah Tovar
Knowing Android

Google Developer Expert on Android | Doing Kotlin | Making Software 24/7 | Kotlin Multiplatform | She/Her | Opinions are my own, and not my employer