Search code examples
androidactivity-lifecycleapplication-lifecycle

Determine if application on foreground - is that frowned upon?


There are lots of reasons why detecting if application is on foreground. for example - as a trigger to GCM/C2DM push notification - lot's of apps would have good reason implementing different behavior when app is foreground and background. Other reason could be - shotting down services consuming precious resources, such as querying servers in background task for instance.

Just to be clear: the definition (as I see it) for background app is:
application which none of it activities are invoked the onStart() method, and did not invoke yet the onStop() method. that's because activity is visible to the user in it life cycle only at that time.

From the other hand -

  • seems like Google don't want application to react to the home button (it's not part of the API)

  • reacting to the onBackPressed() on the "root / main" activity as indicator for leaving Activity certainly not good idea (because lots of users using the home button, and not the back button)

  • there is no method in the API allowing determine if app is foreground (according to my definition..)

if I didn't miss something in the API, and it's really the case - Why there is no why to determine easily if the application is foreground or not????!!!!

what I know I can do to determine if the application is foreground is described in this thread - How to detect when an Android app goes to the background and come back to the foreground

but as @Emil saying - it's requiring special permission, or requiring some tricky logic's which very fast becoming problematic to maintain, and it smells like bad approach (although that's what I'm doing for now, because I don't have better idea...)

my questions basically are:

  • Is there no such API method from good reason?

  • Is taking into account if application is foreground or not is a bad approach?

  • Is there any other way to know if application is foreground or not?


Solution

  • With the new Android Architecture Components there is an easy way to know if your app is in the foreground or the background.

    Just like with the activity scope lifecycle owner there is a general process lifecycle owner which you can subscribe to and get lifecycle updates.

    For example:

    Add this code in order to register as a lifecycle observer

    ProcessLifecycleOwner.get().lifecycle.addObserver(lifecycleListener)
    

    And this code in order to receive the relevant callbacks

    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    public void onApplicationOnStartEvent() {
    
    }
    
    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    public void onApplicationOnStopEvent() {
    
    }
    

    Don't forget to remove the observer once you don't need it

    ProcessLifecycleOwner.get().getLifecycle().removeObserver(lifecycleListener);
    

    More information and examples can be found in this excellent article:

    https://proandroiddev.com/detecting-when-an-android-app-backgrounds-in-2018-4b5a94977d5c