I am receiving a notification and i want to create a custom back stack so the user can navigate through it.But as of now clicking on the notification opens the desired activity but when i press the back button it completely exits the app.
Intent resultIntent = new Intent(this, NotifViewActivity.class);
resultIntent.putExtra(StringHolder.NOTIFICATION_ID, notif.getId());
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(HomeActivity.class);
stackBuilder.addParentStack(NotifActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder notificationCompat = new NotificationCompat.Builder(context)
.setAutoCancel(true)
.setContentTitle(notif.getTitle())
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(resultPendingIntent);
Manifest File
<activity
android:name=".NotifActivity"
android:parentActivityName=".HomeActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".HomeActivity" />
</activity>
<activity
android:name=".NotifViewActivity"
android:parentActivityName=".NotifActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".NotifActivity" />
</activity>
The way i want it to work is,on click of the notification the user is taken to NotifViewActivity then when back button is pressed the user is taken to NotifActivity and when back button is pressed again the user is taken to HomeActivity .Thats the hierarchy i am trying to create,how can i do that?
You should build your task stack that way:
stackBuilder.addParentStack(HomeActivity.class);
stackBuilder.addParentStack(NotifActivity.class);
stackBuilder.addNextIntentWithParentStack(resultIntent);
Or actually because you already specifying activity hierarchy in manifest, you can do it with just one line:
stackBuilder.addNextIntentWithParentStack(resultIntent);
Or another way to archive the same without specifying hierarchy in manifest:
Intent mainActivityIntent = new Intent(this, HomeActivity.class);
Intent notifActivityIntent = new Intent(this, NotifActivity.class);
stackBuilder.addNextIntent(mainActivityIntent);
stackBuilder.addNextIntent(notifActivityIntent);
stackBuilder.addNextIntent(resultIntent);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With