我已閱讀了圍繞此問題的多個問題和答案,但是我無法讓他們中的任何人爲我工作。通知恢復應用程序,而不是重新啓動
我有一個通知,點擊時我想將應用程序放在前面並繼續而不是關閉並重新啓動。
這是我的通知代碼
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