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

旧时模样 提交于 2019-12-01 01:08:49
Awais Tariq

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

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 :)

V.J.
    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...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!