2

I'm trying to work with the notification in android, and what I want to happen is that whenever I click the notification it will open the new activity, and will automatically close the activity I was previous on. I tried adding some flags but it didn't work, it keeps on pilling up the activity on top of the other. Any suggestion on how to make this work will be appreciated.

Here is the snippet of my codes:

protected void displayNotification() {
        Intent i = new Intent(context, Order.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

        notification = new NotificationCompat.Builder(context);
        notification.setContentTitle("Received New Order/s");
        notification.setContentText("Refresh Order List");
        notification.setTicker("Receive New Order/s");
        notification.setDefaults(Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND);
        notification.setWhen(System.currentTimeMillis());
        notification.setSmallIcon(R.drawable.my_icon);
        notification.setContentIntent(PendingIntent.getActivity(context, 0, i, PendingIntent.FLAG_UPDATE_CURRENT));
        notification.setAutoCancel(true);

        NotificationManager nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
        nm.notify(uniqueID,notification.build());

    }

1 Answers1

6

Use this:

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
|Intent.FLAG_ACTIVITY_CLEAR_TASK);

see if it helps

Abhishek Jaiswal
  • 628
  • 6
  • 17
  • It worked. thanks alot, but I want to know what is the downside of doing this? – newBieUser0829 Sep 23 '16 at 18:18
  • 1
    By doing this we are creating a new task for activity back stack and killing the old task. So your activity is always at the root of the new task. As per your requirement this is the solution. – Abhishek Jaiswal Sep 23 '16 at 19:37