java代碼:
mBuilder = new NotificationCompat.Builder(this).setContent(view)
.setSmallIcon(R.drawable.icon).setTicker("新資訊")
.setWhen(System.currentTimeMillis())
.setOngoing(false)
.setAutoCancel(true);
Intent intent = new Intent(this, NotificationShow.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(NotificationShow.class);
stackBuilder.addNextIntent(intent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(pendingIntent);
AndroidManifest.xml中設(shè)置android:parentActivityName屬性值.
<activity
android:name="com.shulf.notificationtest.NotificationShow"
android:parentActivityName=".MainActivity" >
</activity>
這樣月弛, 當(dāng)通知欄被點(diǎn)擊后, 顯示NotificationShow Activity, 當(dāng)點(diǎn)擊back時, 不會回到桌面忆某, 而是進(jìn)入app的主頁面.
之前要想達(dá)到同樣目的, 需要在Activity中記錄一個flag值, 用來標(biāo)記這個Activity是從notification打開的篓吁, 然后在Activity的 onBackPressed()中做處理導(dǎo)航進(jìn)入MainActivity.
----DONE.---------