简体   繁体   English

开始新活动时完成2个活动

[英]Finish 2 activities when starting new activity

I have 4 activities: ABCD When starting the activity D from C, B and C should be removed from the stack. 我有4个活动:ABCD从C开始活动D时,应从堆栈中删除B和C。 So, the sequence is like: A > B > C > D then D > A What I've tried doing when starting D from C is: 因此,顺序如下:A> B> C> D然后D> A从C启动D时,我尝试做的是:

//create new Intent(C,D)
//setflags Intent.FLAG_ACTIVITY_CLEAR_TOP
//startActivity
//finish C

When onBackPressed is called in D, activity B is displayed. 在D中调用onBackPressed时,将显示活动B。 Seems like it didn't finish. 好像没有完成。 And when checking the stack of activities in Layout Inspector I can see AB D. What do I do to remove B from stack upon calling D or upon onBackPressed of D? 在Layout Inspector中检查活动堆栈时,我可以看到ABD。在调用D或D的onBackPressed时如何从堆栈中删除B? Note that I need B when C is started that's why I didn't try calling finish() when starting C. Thanks in advance 请注意,启动C时我需要B,这就是为什么我在启动C时没有尝试调用finish()的原因。

How about loading A Activity from D Activity. 如何从D活动加载活动。

Intent intent = new Intent(getBaseContext(), A.class);
startActivity(intent); 

One way to achieve this is when you press back on the activity D, you simply start your activity A with the flag FLAG_ACTIVITY_CLEAR_TOP . 实现此目的的一种方法是,当您按回活动D时,只需使用标志FLAG_ACTIVITY_CLEAR_TOP来开始活动A。 This will bring the already existing activity A to the front and clear all other activities. 这会将已经存在的活动A放在最前面,并清除所有其他活动。

@Override
void onBackPressed() {
     Intent intent = new Intent(this, A.class);
     intent.setFlags(FLAG_ACTIVITY_CLEAR_TOP);
     startActivity(intent);
}

Note: This will also clear the activity D from the stack so you won't be able to press back and go to the activity D. 注意:这还将从堆栈中清除活动D,因此您将无法按回去活动D。

只需使用:

startActivity(new Intent(this, A.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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