[英]Keep the root activity even after starting new activity and clearing the back stack
I will be starting activities in the following series:我将开始以下系列的活动:
A->B->C->D Now I want to start another activity suppose E from D and clear the stack but keep activity A as root activity. A->B->C->D 现在我想从 D 开始另一个活动,假设 E 并清除堆栈,但将活动 A 保留为根活动。 After starting E the stack should be A->E.
启动 E 后,堆栈应为 A->E。 How can I achieve this?
我怎样才能做到这一点?
You can acheive that with TaskStackBuilder .您可以使用TaskStackBuilder 实现这一点。 This dude lets you rebuild stack which you need.
这个家伙让你重建你需要的堆栈。 You need somethink like this:
你需要这样的想法:
final Intent activityAIntent = new Intent(this, ActivityA.class);
activityAIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
TaskStackBuilder.create(this)
.addNextIntent(activityAIntent)
.addNextIntent(new Intent(this, ActivityE.class))
.startActivities();
When your start activity E from D first clear flag top Or finishAffinity().当您从 D 开始活动 E 时首先清除标志顶部或完成 Affinity()。 So all your previous activity is closed.
因此,您之前的所有活动都已关闭。 and open first activity from backpress.
并从 backpress 打开第一个活动。
Add OnBackPressed method in E Activity.在 E Activity 中添加 OnBackPressed 方法。
Like this像这样
@Override
public void onBackPressed() {
super.onBackPressed();
Intent intent = new Intent(getApplicationContext(), AActivity.class);
startActivity(intent);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
finishAffinity();
} else {
finish();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.