Clear Activity back stack [duplicate]

六月ゝ 毕业季﹏ 提交于 2019-11-29 07:00:36

You could add a BroadcastReceiver in all activities you want to close (A, B, C, D):

public class MyActivity extends Activity {
    private FinishReceiver finishReceiver;
    private static final String ACTION_FINISH = 
           "com.mypackage.MyActivity.ACTION_FINISH";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        finishReceiver= new FinishReceiver();
        registerReceiver(finishReceiver, new IntentFilter(ACTION_FINISH));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        unregisterReceiver(finishReceiver);
    }

    private final class FinishReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(ACTION_FINISH)) 
                finish();
        }
    }
}

... and close them by calling ...

sendBroadcast(new Intent(ACTION_FINISH));

... in activity E. Check this nice example too.

Add flag FLAG_ACTIVITY_CLEAR_TOP to your intent to clear your other Activities form Back stack when you are starting your E Activity like :

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

then start your Activity :

startActivity(intent)

More Information on : Task and BackStack

Add flags to your itent it will clear all activities in a stack

Intent intent = new Intent(getApplicationContext(),MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |  Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

this is the right wat to clear back activities already in a stack

Hope this helps..

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