Java 如何在Android中调出活动后台堆栈中存在的活动?

Java 如何在Android中调出活动后台堆栈中存在的活动?,java,android,android-activity,Java,Android,Android Activity,假设我有一个名为ActivityA的活动和另一个名为ActivityB的活动。在每一个活动中,我都有一个按钮,单击该按钮可打开另一个活动。单击按钮时,我要执行以下工作: 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

假设我有一个名为
ActivityA
的活动和另一个名为
ActivityB
的活动。在每一个活动中,我都有一个按钮,单击该按钮可打开另一个活动。单击按钮时,我要执行以下工作:

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将创建一个新实例并将其放在堆栈顶部。

尝试在清单中为单个实例添加启动模式对Zach的评论是肯定的,文档是@ZachBublil,它不能解决我的所有问题。我想在后堆栈中保留另一个活动,只想将目标活动放在前面。@请阅读我的评论。如我所说,请尝试AndroidManifest中的启动模式选项。简言之,它定义了活动的启动方式-如果后台堆栈中已经有此活动的实例,则“单实例”会使应用程序将活动置于前端,如果没有,则创建实例。