一般的写法,点击通知栏进入的Activity是一个新创建的Activity,而不是原先正在运行的Activity,这和我的想法是背道而驰的。当你点击返回按键退出这个Activity之后,发现,原先正在运行的Activity终于出现了。明显这样是不符合条理的。
我们想要点击通知后返回的是正在运行的活动(如果活动正在运行)或者创建新的活动(活动已经停止),应该这样写:
只有在设置PendingIntent这里稍微设置一下就可以。
一般的写法:
Intent notificationIntent = new Intent(this, MyActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.contentIntent = contentIntent;
notification.flags=Notification.FLAG_AUTO_CANCEL;
NotificationManager manager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(NOTIFY_ID, notification);
下面是稍微设置了的:
// 设置启动的程序,如果存在则找出,否则新的启动
Intent intent = new Intent(Intent.ACTION_MAIN)
intent.addCategory(Intent.CATEGORY_LAUNCHER)
intent.setComponent(new ComponentName(this, MainActivity.class))
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED)
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0)
notification.contentIntent = contentIntent
notification.flags=Notification.FLAG_AUTO_CANCEL
NotificationManager manager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE)
manager.notify(NOTIFY_ID, notification)