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

后端 未结 3 1462
旧时难觅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:51
        Intent myintent = new Intent(YourCurrentActivity.this, YourNextActivitys.class);
        myintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(myintent);
        finish();
    

    I think this is the right way to clear all previous activities...

    0 讨论(0)
  • 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 :)

    0 讨论(0)
  • 2021-01-07 01:01

    Make a custom Broadcast receiver and register it in every activity which can be fired on event of your choice. in onReceiveMethod of every activity (may be selected )just call finish(). In this your activities will be removed from the stack. Further you can visit this for more help: On logout, clear Activity history stack, preventing "back" button from opening logged-in-only Activites

    0 讨论(0)
提交回复
热议问题