1

I wrote my notification intent like this(the snippet below). I have Activity A,B and C running. While running C, I pressed the home screen and soon received a notification. I pressed on the notification icon, hoping restart the app from Activity A but unfortunately it doesn't. What this code current do is create a new Activity A on top of the stack.... So I am stuck with the following running activity(or stack):A,B,C,A

So my main question is, how can I clean up the Activity stack so that only Activity A is on the stack when the notification icon is clicked?

Any tips or comments would appreciated.

  Intent notificationIntent = new Intent(context,
                A.class);

        notificationIntent.setAction(Intent.ACTION_MAIN);
        notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                | Intent.FLAG_ACTIVITY_CLEAR_TOP);

  PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
                notificationIntent, 0);

        notification.setLatestEventInfo(appContext, contentTitle,
                contentText, contentIntent);

        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager mNotificationManager = (NotificationManager) context
                .getSystemService(ns);


  mNotificationManager.notify(1, notification);
xiaowoo
  • 2,248
  • 7
  • 34
  • 45

2 Answers2

0

Set launchMode="singleTask" for the activity A in the manifest. In this case activity A will be the only one running after you go back to it from notification.

However when launch mode is singleTask or singleInstance your app is going to behave the same way when you resume your app from the background. It's not going to be possible to resume to activity B or C.

vitalnik
  • 568
  • 6
  • 10
0

This question may help you either with FLAG_ACTIVITY_CLEAR_TOP or using startActivityForResult.

Community
  • 1
  • 1
MrZander
  • 3,031
  • 1
  • 26
  • 50