tags:

views:

57

answers:

2

The official documentation describes tasks as follows:

*All the activities in a task move together as a unit. The entire task (the entire activity stack) can be brought to the foreground or sent to the background. Suppose, for instance, that the current task has four activities in its stack — three under the current activity. The user presses the HOME key, goes to the application launcher, and selects a new application (actually, a new task). The current task goes into the background and the root activity for the new task is displayed. Then, after a short period, the user goes back to the home screen and again selects the previous application (the previous task). That task, with all four activities in the stack, comes forward.

Is there a way to programmatically detect when the task of the current Activity moves into and out of the background? I would like to know when the user has switched switched to another application, vs. when the user navigated to another Activity in the current app.

+1  A: 

Note: I've always had this design in my head but never got around to concrete tests (reasonably sure it works though), so if you end up trying this, tell me how it goes. :)


I don't think there's an official API method that'll give you this functionality (I may be wrong), but you can certainly construct your own. This is just my way of doing it; there may, of course, be better implementations.

In your application, you always know when you're starting another activity. With that said, you can create a global class that handles starting all of your application activities and keeps track of a boolean that represents when you're starting an activity:

public class GlobalActivityManager {

    private static boolean isActivityStarting = true;

    public static boolean isActivityStarting() {
        return isActivityStarting;
    }

    public static void setIsActivityStarting(boolean newValue) {
        isActivityStarting = newValue;
    }

    public static void startActivity(Activity previous, Class activityClass) {
        isActivityStarting = true;
        // Do stuff to start your activity
        isActivityStarting = false;
    }
}

Then in each of your activities (maybe make this a superclass), override the onPause() method to check if it's another one of your application's activities starting or a foreign task's application coming in front of your application. This works because onPause is executed when anything comes in front of the activity, meaning it's called when you start your activity in GlobalActivityManager where isActivityStarting is true.

public void onPause() {
    super.onPause();
    if (GlobalActivityManager.isActivityStarting()) {
        // It's one of your application's activities. Everything's normal.
    } else {
        // Some other task has come to the foreground. Do what needs to be done.
    }
}
Andy Zhang
A: 

That will not work. Your activity could get an onPause for reasons other than your app explicitly starting another activity. For instance, the user could have hit the back button, taking you back to a previous Activity in your stack.

james