what is the right way to clear background activity/activites from stack?

后端 未结 3 1463
旧时难觅i
旧时难觅i 2021-01-07 00:30

as the questions title says - I need to know what is the best way to \"remove\"/destroy/finish an activity that are somewhere in the middle of stack and currently on pause

3条回答
  •  独厮守ぢ
    2021-01-07 00:56

    I also had the same problem. What I did is, I kept a static array list, and whenever I used to go from one activity to another, in the onCreate() method of new activity, I added the object of current activity into that list like this:

    SomeClass.addActivity(CurrentActivity.this);
    

    I added the above statement in each activity.

    The addActivity():

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

    And when I wanted to clear the stack, I called:

    public boolean clearStack() {
            for (Activity activity : activityList) {
                activity.finish();
            }
            activityList.clear();
            return (activityList.isEmpty());
        }
    

    In this way, I cleared my activity stack.

    Thank you :)

提交回复
热议问题