gpt4 book ai didi

android - 管理android的backstack

转载 作者:太空宇宙 更新时间:2023-11-03 10:41:00 24 4
gpt4 key购买 nike

我有一个 Activity 层次结构,并且有一个按钮可以让我从 Activity D 转到 Activity B。

image

问题是从 D 转到 B 会使 C 留在后台,所以如果我执行 A->C->D->B 然后按回它会把我送到 C,而不是 A(哪个是我想要的)。

有没有办法在我点击 B 的按钮时删除 C,或者有某种解决方法?

最佳答案

考虑使用 A 作为调度程序。当您想从 D 启动 B 并在此过程中完成 C 时,请在 D 中执行此操作:

// Launch A (our dispatcher)
Intent intent = new Intent(this, A.class);
// Setting CLEAR_TOP ensures that all other activities on top of A will be finished
// and setting SINGLE_TOP ensures that a new instance of A will not
// be created (the existing instance will be reused and onNewIntent() will be called)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
// Add an extra telling A that it should launch B
intent.putExtra("startB", true);
startActivity(intent);

A.onNewIntent() 中执行此操作:

@Override
protected void onNewIntent(Intent intent) {
if (intent.hasExtra("startB")) {
// Need to start B from here
startActivity(new Intent(this, B.class));
}
}

关于android - 管理android的backstack,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33879719/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com