'假设我有 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/