Search code examples
androidbroadcastreceiveronresumeonpausebackground-foreground

how to detect app is now foreground after minimizing the app?


by using onResume() i can check if an activity is foreground or not. My problem is this onResume() is fire each time even if i come from another activity to this activity.

So my main problem is ,if i minimizing the app by home button I want execute some code in each activity when app is come foreground BUT not using onResume().

I found an answer like this How to know in BroadcastReceiver if App is running on foreground? but i dont know how to register this receiver in android menifest to get the trigger when app is visible.

Please give me some tips how can i overcome this solution or code snippet which can help me. Thanks in advance :)


Solution

  • One thing to do what you want is to count the number of times onStart/onStop is called in your application. This will help you determine if you transitioned to your activity from inside or outside your application.

    You must extend Application then create/register ActivityLifecycleCallbacks within that class. Also, make sure to specify the new Application class you created in the AndroidManifest.

    Now, the trick is to keep a count variable in onActivityStarted/onActivityStopped to determine whether your Activity was navigated to from inside or outside the application.

    Say you have 2 Activities in your app: FirstActivity & SecondActivity.

    If you navigate from FirstActivity to SecondActivity the lifecycle calls will happen in this order: FirstActivity.onStart() > SecondActivity.onStart(), resulting in a count of 1.

    If you navigate from outside your application, you will only see FirstActivity.onStart(), so the count is 0. This is all assuming you check the count after super.onStart() is called.

    So, by checking count against 0/1 you can tell if your activity was launched from within the application or outside the application.

    /**
     * Extending the application class lets you use ActivityLifecycleCallbacks to
     * keep track of all lifecycle callbacks in your application.
     */
    public class MyApplication extends Application implements ActivityLifecycleCallbacks {
        private int count = 0;
    
        //Register activity lifecycle callbacks in onCreate
        public void onCreate() {
            super.onCreate();
            registerActivityLifecycleCallbacks(this);
        }
    
        void onActivityStarted(Activity activity) {
            count++;
        }
    
        void onActivityStopped(Activity activity) {
            count--;
        }
    
        /**
         * Use this method in your Activities to test if the activity was
         * transitioned to from outside the application.
         * 
         * If you call this method in Activity.onResume(), then count should be
         * compared to 0. If you call this method in Activity.onStart() but
         * *before* calling super.onStart(), then count should be compared to 0.
         * 
         * However, if you call this method after super.onStart(), then count
         * should be compared to 1.
         */
        public boolean cameFromOutsideApplication() {
            return count == 0;
        }
    
        //Don't need to use the rest of the activity lifecycle callbacks
        void onActivityCreated(Activity activity, Bundle savedInstanceState) {
        }
        void onActivityDestroyed(Activity activity) {
        }
        void onActivityPaused(Activity activity) {
        }
        void onActivityResumed(Activity activity) {
        }
        void onActivitySaveInstanceState(Activity activity, Bundle outState) {
        }
    }
    

    You may gain some more information here as well but it does not use ActivityLifecycleCallbacks which is easier to use.