2

I've got a simple use case. I have activity open like below: A > B > C

Now, I get a push notification.

I do the following in my GCMIntentService class:

In onMessage,

Intent notificationIntent = new Intent(context, A.class);       
PendingIntent contentIntent = 
PendingIntent.getActivity(context,0, notificationIntent,     
PendingIntent.FLAG_CANCEL_CURRENT);
notification.setContentIntent(contentIntent);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP );

My expectation is that activities B, C should be removed from Activity stack and you should be re-directed to activity A.

What is happening is, however, A gets opened; but when you hit back button I get C, B, A again in that order!!

What is it that I'm doing wrong?

Here are the things I tried:

  1. I've experimented with notificationIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);, notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP, Intent.FLAG_ACTIVITY_SINGLE_TOP);

  2. I've kept a static reference to the last foreground activity and passed that as context.

Nothing seems to work.

pavel
  • 26,538
  • 10
  • 45
  • 61
ranjjose
  • 2,138
  • 1
  • 24
  • 46

3 Answers3

2

Try this. It may help you. Let me know once you have tested it.

        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(notificationIntent);
EnabrenTane
  • 7,428
  • 2
  • 26
  • 44
Arun Antoney
  • 4,292
  • 2
  • 20
  • 26
  • Thanks for the help, but the solution you provided didn't work. Resulted in the same behavior! – ranjjose Jul 09 '14 at 12:05
  • 1
    @ranjjose can you try with those flags, but add them to the `notificationIntent` BEFORE you create the `PendingIntent` – LordRaydenMK Jul 09 '14 at 12:28
  • Thanks @LordRaydenMK. The order was also the issue. Infact I added `notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);` as suggested by @Arun Antoney and then changed the order as suggested by @LordRaydenMK. It worked! Thanks everyone. – ranjjose Jul 09 '14 at 14:00
1

try this Tutorial

Intent notificationIntent = new Intent(context, YourActivity.class);
        // set intent so it does not start a new activity
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
                Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent intent =
                PendingIntent.getActivity(context, 0, notificationIntent, 0);
        notification.setLatestEventInfo(context, title, message, intent);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;

        // Play default notification sound
        notification.defaults |= Notification.DEFAULT_SOUND;

        // Vibrate if vibrate is enabled
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        notificationManager.notify(0, notification);
Kaushik
  • 6,150
  • 5
  • 39
  • 54
  • thanks; but as I've mentioned in the things I've already tried section, I've tried ` notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);`. Moreover I developed push notification refering to the same blog. :) – ranjjose Jul 09 '14 at 12:17
  • We all are using the same method ,please test this in your app once again. – Arun Antoney Jul 09 '14 at 12:39
1

Set launch mode of your activity to "singleTask":

<activity
  android:name=".ActivityA"
  android:launchMode="singleTask"
>

And set flags as below:

Intent intent= new Intent(currentActivity, ActivityA.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
VSB
  • 9,825
  • 16
  • 72
  • 145