Search code examples
onresumeonpauseandroid

Android: "Application level" Pause and Resume


I've been trying to get Application Level Pause and Resume similar to an activity's onPause and onResume. I know there's no API that has this functionality.

I try to follow this post: http://curioustechizen.blogspot.com/2012/12/android-application-level-pause-and.html

But I've had no luck so far.

Has anyone been able to achieve this? What paradigm did you use?

Let me know if you need me to paste some code into this question. Thanks for the help


Solution

  • Another solution to the problem would be to just keep track of the count of onStart() and onStop() calls from every activity. Example:

    First, create a class to hold the counts:

    public class ActiveActivitiesTracker {
        private static int sActiveActivities = 0;
    
        public static void activityStarted()
        {
            if( sActiveActivities == 0 )
            {
                // TODO: Here is presumably "application level" resume
            }
            sActiveActivities++;
        }
    
        public static void activityStopped()
        {
            sActiveActivities--;
            if( sActiveActivities == 0 )
            {
                // TODO: Here is presumably "application level" pause
            }
        }
    }
    

    Then in every activity, simply call the activityStarted() and activityStopped() methods:

    @Override
    public void onStart() {
        super.onStart();
        ActiveActivitiesTracker.activityStarted();
    }
    
    @Override
    public void onStop() {
        super.onStop();
        ActiveActivitiesTracker.activityStopped();
    }