My app is on background or foreground?
Tags: android, kotlin, architecture, background
Published: 2019-08-16 18:18:36

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](https://stackoverflow.com/questions/3667022/checking-if-an-android-application-is-running-in-the-background) or this [another one](https://stackoverflow.com/questions/2166961/determining-the-current-foreground-application-from-a-background-task-or-service). 
This question has been hunting a lot of android developers from a long time, for some time the Android Developers has been doing this:

{% gist https://gist.github.com/dinorahto/a11ca300ad2b06250eec07ba7d48bb62 %}

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](https://developer.android.com/topic/libraries/architecture/lifecycle), 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.

{% gist https://gist.github.com/dinorahto/a7212cb49d3b218d9e9f88228657818a %}

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:

{% gist https://gist.github.com/dinorahto/2b370da8a79df35ab528b88ab4f1ca56 %}

*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