Checking if an Android application is running in the background

前端 未结 30 2804
无人共我
无人共我 2020-11-21 06:19

By background, I mean none of the application\'s activities are currently visible to the user?

30条回答
  •  太阳男子
    2020-11-21 06:55

    I tried the recommended solution that uses Application.ActivityLifecycleCallbacks and many others, but they didn't work as expected. Thanks to Sarge, I came up with a pretty easy and straightforward solution that I am describing below.

    They key of the solution is the fact of understanding that if we have ActivityA and ActivityB, and we call ActivityB from ActivityA (and not call ActivityA.finish), then ActivityB's onStart() will be called before ActivityA onStop().

    That's also the main difference between onStop() and onPause() that none did mention in the articles I read.

    So based on this Activity's Lifecycle behavior, you can simply count how many times did onStart() and onPause() got called in your program. Note that for each Activity of your program, you must override onStart() and onStop(), in order to increment/decrement the static variable used for counting. Below is the code implementing this logic. Note that I am using a class that extends Application, so dont forget to declare on Manifest.xml inside Application tag: android:name=".Utilities", although it can be implemented using a simple custom class too.

    public class Utilities extends Application
    {
        private static int stateCounter;
    
        public void onCreate()
        {
            super.onCreate();
            stateCounter = 0;
        }
    
        /**
         * @return true if application is on background
         * */
        public static boolean isApplicationOnBackground()
        {
            return stateCounter == 0;
        }
    
        //to be called on each Activity onStart()
        public static void activityStarted()
        {
            stateCounter++;
        }
    
        //to be called on each Activity onStop()
        public static void activityStopped()
        {
            stateCounter--;
        }
    }
    

    Now on each Activity of our program, we should override onStart() and onStop() and increment/decrement as shown below:

    @Override
    public void onStart()
    {
        super.onStart();
        Utilities.activityStarted();
    }
    
    @Override
    public void onStop()
    {
        Utilities.activityStopped();
        if(Utilities.isApplicationOnBackground())
        {
            //you should want to check here if your application is on background
        }
        super.onStop();
    }
    

    With this logic, there are 2 possible cases:

    1. stateCounter = 0 : The number of stopped is equal with the number of started Activities, which means that the application is running on the background.
    2. stateCounter > 0 : The number of started is bigger than the number of stopped, which means that the application is running on the foreground.

    Notice: stateCounter < 0 would mean that there are more stopped Activities rather than started, which is impossible. If you encounter this case, then it means that you are not increasing/decreasing the counter as you should.

    You are ready to go. You should want to check if your application is on background inside onStop().

提交回复
热议问题