java - 返回特定 Activity 而不重新开始它

标签 java android android-intent stack

'假设我有 Activity A、B、C、D。 我开始这些 Activity ,如 A->B->C->D 现在我想通过单击按钮返回 A 而不加载其 再次数据。 注意:我不希望这一切都在背压上。

我已经尝试过了。”

Intent intent = new Intent(getActivity(),
A.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

'但原因是它又启动了A。再次调用所有 api。

我想要的就是这个。例如我有两项 Activity ,例如 1,2 我像 1->2 一样开始它们 现在,如果我在按钮上完成 2,则单击 1 将显示给我,而无需 再次开始。我想要通过单击 a 从 D 到 A 得到相同的结果 按钮不在背面按下。'

最佳答案

仅使用清除顶部标志会导致目标 Activity 首先被销毁,然后重新创建。为了避免这种情况,请使用两个标志:

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

这将导致 Activity 无法完成,因此您的 API 调用不会再次发生。在这种情况下,如果您需要,您在此处用于启动 Activity A 的 Intent 将被传递到 Activity A 中的 onNewIntent() 方法。

这一切都假设您没有在使用“多重”启动模式的 Activity list 声明中指定任何启动模式。

根据文档:

If it has declared its launch mode to be "multiple" (the default) and you have not set FLAG_ACTIVITY_SINGLE_TOP in the same intent, then it will be finished and re-created; for all other launch modes or if FLAG_ACTIVITY_SINGLE_TOP is set then this Intent will be delivered to the current instance's onNewIntent().

关于java - 返回特定 Activity 而不重新开始它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57688389/

相关文章:

android - 从 Android 应用程序获取用户反馈的更好方法?

android - 抽屉导航 Android 上的底部布局

android - 如何在没有 singleTop 的情况下使用 Android SearchView?

android.content.ActivityNotFoundException : No Activity found to handle Intent splash screen

java - 编译时和运行时变量的绑定(bind)

java - 我可以使用线程在 JavaFX 中设置标签文本吗?

java - 从 equals() 调用 hashCode()

java - 在 GWT 中使用模拟堆栈有多少开销?

java - Java 中 Intent 之间的困难 (Android Studio)

android - 如何通过 ContentProvider URI 获取从 Gallery 发送到我的应用程序的视频的实际路径?