如何从使用startForeground创建的通知中重复使用顶部活动?

3
我有一个服务,当用户执行某个操作时,它会使用Service.startForeground(..)在前台运行,因为杀死它会对用户造成干扰。
我使用以下代码构建通知来创建其待定意图:
Intent topActivityIntent = new Intent(this, topActivityClass);
// Rebuild the task stack
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack
stackBuilder.addParentStack(topActivityClass);
stackBuilder.addNextIntent(topActivityIntent);
for (int i = 0; i < stackBuilder.getIntentCount(); i++) {
    Intent currentIntent = stackBuilder.editIntentAt(i);
    // Set flags to tell that we want to re-use the existing
    // topActivity if possible
    currentIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | 
        Intent.FLAG_ACTIVITY_SINGLE_TOP | 
        Intent.FLAG_ACTIVITY_NEW_TASK);
}
// Adds the Intent to the top of the stack
// Gets a PendingIntent containing the entire back stack
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0,
    PendingIntent.FLAG_UPDATE_CURRENT);
topActivityClass是用户当前正在查看的活动,因为我希望他/她返回到相同的活动。
问题在于,当用户点击通知时,会创建一个新的Activity,而不是使用任务栈中的现有Activity
我尝试过仅对topActivityIntent设置标志以及多种标志组合。
应该使用哪些标志来避免创建新活动?是否可以重复使用堆栈中存在的活动?
1个回答

0

只需从意图中删除FLAG_ACTIVITY_NEW_TASK,它就会重用现有任务


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接