1

I just saw a piece of code:

public class MyApplication extends Application {

        private List<Activity> activityList = new LinkedList<Activity>();
        private static MyApplication instance;

        private MyApplication() {
        }

        public static MyApplication getInstance() {
                if (null == instance) {
                        instance = new MyApplication();
                }
                return instance;

        }


        public void addActivity(Activity activity) {
                activityList.add(activity);
        }


        public void exit() {

                for (Activity activity : activityList) {
                        activity.finish();
                }

                System.exit(0);

        }

}

I never thought that we can take control of other activity beside the current one. I usually call finish() inside its own activity, now I saw this code, I realize that we can finish() other activity as well.

Android stack is back stack architecture, so if I destroy any activity in the middle, what will happen? For example, I have 5 activity in the back stack, let say I finish() the third one, will the second and fourth be linked together now?

4

1 回答 1

1

Android maintains a stack of the Activities being started, so the application which was started first goes to the bottom of the stack, the second one above it and henceforth.

So, if you remove the third Activity, the fourth one would come on top of the second Activity and thus they would get linked, as you have rightly understood from the code.

于 2012-10-22T02:59:47.100 回答