Android 在特定 Activity 之上启动 Activity

标签 android android-activity back-stack

假设我有以下 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 并在此过程中完成 CB 时,请在 中执行此操作>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/

相关文章:

java - Android检测应用程序何时暂停

android - 在其他 Activity 中传递 Mediaplayer

android - 将数据从 Android Activity 发送到 React Native

android - 如何在android中获取backstack fragment 条目列表?

android - 更改 Android 上 Activity 动画的 z 顺序

android - 无法使用 AltBeacon/android-beacon-library 检测 iBeacon

android - Android Studio 0.4.2错误[ContentElementType:FILE;文件:HtmlFile:incognito_mode_start_page.html]

android - 切换 android map fragment -- 崩溃

android - 如何清除支持FragmentManager的backStack?

android - 卡片 View 缩放动画