How to preserve current back stack (or task) when notification is clicked?
I found this link: Preserving Navigation when Starting an Activity. Although it does not provide the exact solution for my question, but it produces the desired result.
The link describes that we should start DetailsActivity
in a new task with a different affinity.
Manifest:
<activity
android:name=".DetailsActivity"
android:excludeFromRecents="true"
android:label="@string/app_name_system"
android:launchMode="singleTop"
android:taskAffinity=""
android:theme="@style/AppTheme.NoActionBar" />
PendingIntent
creation:
Intent intent = new Intent(context, DetailsActivity.class);
intent.putExtra(Com.KEY_ID, event.getId());
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
You can use PendingIntent.getActivities
instead of PendingIntent.getActivity
to build you stack.
Intent mainActivityIntent = new Intent(context,MainActivity.class);
mainActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Intent noteActivityIntent= new Intent(context,NoteActivity.class);
noteActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Intent detailActivityIntent= new Intent(context,DetailActivity.class);
final PendingIntent pendingIntent = PendingIntent.getActivities(ctx, UNIQUE_REQUEST_CODE++,
new Intent[] {mainActivityIntent,noteActivityIntent,detailActivityIntent},PendingIntent.FLAG_UPDATE_CURRENT);
This should solve your purpose.
Refer this link for more details Back to main activity from notification-created activity