I\'m trying to implement the behaviour described here, where a notification (or whatever) starts an \"internal\" activity in your app, and then when the user pressed back it
just add this lines to your manifest.
<activity
android:name=".YourActivity"
android:launchMode="singleTask"
android:taskAffinity=""
android:excludeFromRecents="true">
</activity>
for a complete guideline refer to https://developer.android.com/training/notify-user/navigation
Intent displayIntent = new Intent(getApplicationContext(), TargetActivity.class);
displayIntent.putExtra("extra_id", "extra_key");
TaskStackBuilder stackBuilder = TaskStackBuilder.create(getApplicationContext());
stackBuilder.addParentStack(TargetActivity.class);
stackBuilder.addNextIntent(displayIntent);
stackBuilder.editIntentAt(1).putExtra("extra_id", "extra_key");
PendingIntent contentIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
getApplicationContext(), "123").setSmallIcon(R.drawable.logo)
.setContentTitle("GCM Notification")
.setStyle(new NotificationCompat.BigTextStyle().bigText("Sample")).setContentText("sample text");
mBuilder.setContentIntent(contentIntent);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(456, mBuilder.build());
stackBuilder.startActivities();
In addition to the above code also provide appropriate parent class names in your AndroidManifest.xml
file.
I know I'm late but this may help someone.
Just create an intent for the activity you want to open with the notification.
Then set Intent.FLAG_ACTIVITY_NEW_TASK
and Intent.FLAG_ACTIVITY_CLEAR_TASK
as flags.
Then all you need to do is to create a PendingIntent
with your intent as parameter.
This is a code example:
Intent intent = new Intent(this, NotificationActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);