newBieUser0829 newBieUser0829 - 2 months ago 12
Android Question

How to close previous activity after the notification is clicked and opens a new activity

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());

}

Answer

Use this:

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

see if it helps

Comments