How do you build an Android back stack when an activity is started directly from a notification?
You can add an Extra into the Intent launched by the notification to detect when the app has been launched in that way.
Then you can override the onBackPressed()
Activity method and handle that scenario, e.g.
@Override public void onBackPressed() { Bundle extras = getIntent().getExtras(); boolean launchedFromNotif = false; if (extras.containsKey("EXTRA_LAUNCHED_BY_NOTIFICATION")) { launchedFromNotif = extras.getBoolean("EXTRA_LAUNCHED_BY_NOTIFICATION"); } if (launchedFromNotif) { // Launched from notification, handle as special case Intent intent = new Intent(this, ActivityA.class); intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); mActivity.startActivity(intent); finish(); } else { super.onBackPressed(); } }
You should take care of this when you receive the Notification.
I have a similar situation solved:
Intent intent = new Intent(context,ListDetail.class); TaskStackBuilder stackBuilder = TaskStackBuilder.create(context); stackBuilder.addParentStack(ListDetail.class); stackBuilder.addNextIntent(intent); PendingIntent contentIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); NotificationManager mNotifM = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); Notification.Builder mBuilder = new Notification.Builder(context); mNotifM.notify(NotificationId.getID(), mBuilder.setStyle(new Notification.BigTextStyle(mBuilder) .bigText(bigText) .setBigContentTitle(title) .setSummaryText(summaryText)) .setContentTitle(title) .setSmallIcon(icon) .setContentText(summaryText) .setAutoCancel(true) .setContentIntent(contentIntent) .setTicker(bigText) .build());
You need to set in your Manifest the hierarchy of the Activities:
<activity android:name=".ListDetail" android:label="Detail" android:parentActivityName=".List" > <meta-data android:name="android.support.PARENT_ACTIVITY" android:value=".List" /></activity>
I have tried one sample.Please go through with this link
https://github.com/rajajawahar/NotificationBackStack
Activity you want to launch..
Intent launchIntent = new Intent(context, SecondActivity.class).putExtra("Id", id);
Parent Activity, if back pressed
Intent parentIntent = new Intent(context, FirstActivity.class);
Add Both the activity in the taskbuilder
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context); PendingIntent resultPendingIntent = stackBuilder.addNextIntentWithParentStack(parentIntent).addNextIntent(launchIntent).getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Builder notificationCompatBuilder = new NotificationCompat.Builder(context); NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); mNotificationManager.notify(id, notificationCompatBuilder.build()); NotificationManager mNotifyMgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); notificationCompatBuilder.setAutoCancel(true). setContentTitle("First Notification"). setContentText("Sample Text"). setSmallIcon(R.mipmap.ic_launcher). setContentIntent(resultPendingIntent); mNotifyMgr.notify(id, notificationCompatBuilder.build());