Android: How to detect if current stack of activities (task) moves to background?

前端 未结 8 1573
一生所求
一生所求 2020-12-09 14:24

The official documentation describes tasks as follows:

*All the activities in a task move together as a unit. The entire task (the entire activity sta

相关标签:
8条回答
  • 2020-12-09 14:40

    Try to read about android:finishOnTaskLaunch

    http://developer.android.com/guide/topics/manifest/activity-element.html#finish

    It solved my similiar problem ;-)

    0 讨论(0)
  • 2020-12-09 14:41

    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.
        }
    }
    
    0 讨论(0)
  • 2020-12-09 14:46

    I've been looking for a good answer to this question. It seems Andy Zhang had a good idea, but it may need a bit more work to handle other things, like the Back button.

    Another approach which seems promising is to use the ActivityManager.getRunningAppProcesses() method, which will give you a list of RunningAppProcessInfo which each have an "importance" variable which can be used to detect if your process is in the foreground or other states.

    The problem of course is there is no Detection technique here, just a way to tell what is running and its state, but no way to get informed when the state changes.

    So possibly just run a background thread to poll this info. Seems a bit kludgy but it might work.

    My app uses location services (GPS) so it automatically gets called when the location changes, and I can check the RunningAppProcessInfo list when this happens to determine if my App is still in the foreground. Seems like a lot of work, so maybe Andy's approach can be improved on.

    0 讨论(0)
  • 2020-12-09 14:48

    Maybe this can be helpfull, tell me if it worked for you. only when you return from background the value of activities would be 0 (zero) the rest of the time would be a number higher than 0(zero) when the onRestart() is executed.

    public class FatherClass extends Activity {
    
    private static int activities = 0;
    
    public void onCreate(Bundle savedInstanceState, String clase) {
        super.onCreate(savedInstanceState);
    }
    
    protected void onRestart()
    {
        super.onRestart();
        if(activities == 0){
            Log.i("APP","BACK FROM BACKGROUND");
        }
    }
    
    protected void onStop(){
        super.onStop();
        activities -= 1;
    }
    
    protected void onStart(){
        super.onStart();
        activities += 1;
    }
    

    }


    All of your classes must extend from this class for this to work.

    0 讨论(0)
  • 2020-12-09 14:48

    I have been using the method I describe here:

    Simple check for Android application backgrounding

    It has been working wonderfully for me.

    0 讨论(0)
  • 2020-12-09 14:51

    you might be able to use the isFinishing() method in onPause(). That will tell you if the application is actually finishing or is just being paused. When your task stack goes to the background, the activities aren't finished, they are just paused. This will also get called if you start another activity from this one though, so you would have to check for that.

    @Override
    public void onPause() {
        super.onPause();
        if(!isFinishing()) {
            //app going to background
        }
    }
    
    0 讨论(0)
提交回复
热议问题