假设我有一个名为 ActivityA
的 Activity 和另一个名为 ActivityB
的 Activity 。在每个 Activity 中,我都有一个按钮,单击该按钮即可打开另一个 Activity 。我想在单击按钮时执行以下工作:
check if there is an existing type of the target Activity in the activity back-stack or not, if there is, bring that Activity to the top and if not create new Intent and then go to that Activity.
How can I implement this?
谢谢。
最佳答案
简单。
Intent intent = new Intent(this, TargetActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
这正是你想要的。如果任务堆栈中已经存在 TargetActivity
的实例,则该实例将被重新排列并移至堆栈的顶部(前面)。如果没有 TargetActivity
的现有实例,Android 将创建一个新实例并将其放在堆栈顶部。
关于java - 如何调出 Android 中 Activity 后台堆栈中存在的 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59045860/