简体   繁体   中英

How to clear current activity stack when start another activity which belong to another stack

For example:A start B, B start C, A and C belongs to 'Hello'-stack, and B belongs to 'World'-stack, when user press back button in Activity C, it will return to A. Note1:B means a lot of activities , not just one activity, like A start B1,B1 start B2, B2 start B3....,Bn start C. Note2:I need Bs remain in stack until C has been launched , when user press back in B3, it should return to b2. 在此处输入图片说明

I actually have implemented this need by using startActivityForResult,and I just want to know is there any way using stack to implements this.

从B启动活动C时,只需调用finish()即可。然后在活动C中覆盖onBackPressed()。并添加代码以启动活动A。

When going from activity A to activity B, go as below:

 Intent i=new Intent(ActivityA.this,ActivityB.class);
 startActivity(i);

And as you want to open C from B but don't want B to be in stack, so do as below:

 Intent i=new Intent(ActivityB.this,ActivityC.class);
 startActivity(i);
 ActivityB.this.finish();

So automatically when backpressed on C, you will get A and not B.

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); or intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); works for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM