0

当我点击通知栏时,它会跳过MainActivity.class界面:

Intent notificationIntent = new Intent(context,MainActivity.class); //After click the notification, it will skip to the activity 
PendingIntent contentIntent = PendingIntent.getActivity(context,0,notificationIntent,0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

如果MainActivity.class本身已经打开,我再次点击通知栏,它显示两个界面。谁知道如何解决这个问题?

4

1 回答 1

1

更改您的线路:

Intent notificationIntent = new Intent(context,MainActivity.class);

至:

Intent notificationIntent = 
    new Intent(context,MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

正如Android文档中所说:

FLAG_ACTIVITY_SINGLE_TOP
如果设置,如果 Activity 已经在历史堆栈的顶部运行,则不会启动它。

或者,如果这不是预期的行为,请检查此标志:

FLAG_ACTIVITY_CLEAR_TOP
如果设置,并且正在启动的 Activity 已经在当前任务中运行,那么不会启动该 Activity 的新实例,而是关闭它上面的所有其他 Activity,并且此 Intent 将被传递到(现在在顶部)旧活动作为新意图。

在尝试了解这些标志将导致什么时,您可能会感兴趣以下文档:

于 2013-02-21T10:11:50.213 回答