我已阅读了围绕此问题的多个问题和答案,但是我无法让他们中的任何人为我工作。通知恢复应用程序,而不是重新启动
我有一个通知,点击时我想将应用程序放在前面并继续而不是关闭并重新启动。
这是我的通知代码
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("example")
.setContentText("example");
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this, MainActivity.class);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(MainActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int mId = 0;
// mId allows you to update the notification later on.
mNotificationManager.notify(mId, mBuilder.build());
而在我的清单文件我有
android:launchMode="singleTop"
有人能看到什么错误?我没有收到任何错误,但通知拒绝恢复应用程序,而是重新启动它。
的http:// stackoverflow.com/questions/2232238/how-to-bring-an-activity-to-foregro未堆栈顶部 – nekavally
** resultIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); **仍然导致应用程序重新启动。 – rossd
也许,你试图带到前面的活动已经被摧毁了? – nekavally