假设我有以下 Activity
Activity A, Activity B, Activity C and Activity D
当前堆栈有以下条目
Activity C
Activity B
Activity A
我必须启动Activity D
,以便堆栈变得如下所示,
Activity D
Activity A
我必须设置什么标志?
最佳答案
考虑使用A
作为调度程序。当您想要从 C
启动 D
并在此过程中完成 C
和 B
时,请在 中执行此操作>C
:
// 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 D
intent.putExtra("startD", true);
startActivity(intent);
在A.onNewIntent()
中执行以下操作:
@Override
protected void onNewIntent(Intent intent) {
if (intent.hasExtra("startD")) {
// Need to start D from here
startActivity(new Intent(this, D.class));
}
}
关于Android 在特定 Activity 之上启动 Activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34314285/