newBieUser0829 newBieUser0829 - 1 month ago 4x
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.setContentIntent(PendingIntent.getActivity(context, 0, i, PendingIntent.FLAG_UPDATE_CURRENT));

NotificationManager nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);



Use this:


see if it helps