1

My problem is quite simple to explain:

I create a notification from a push service and start my activity with some extras:

notificationIntent = new Intent(getBaseContext(), MainActivity.class);
                notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                        | Intent.FLAG_ACTIVITY_SINGLE_TOP);
notificationIntent.putExtra("typeI",data.get("type"));

Log.e("DEBUG","PUT EXTRA");

PendingIntent pi = PendingIntent.getActivity(getBaseContext(), 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

So, when I click my notification, I can read the type in my activity:

Log.e("DEBUG", "Extra -> " + getIntent().getStringExtra("typeI"));

And this is working fine.

The only annoyance is that I quit the application with the back button, Activity is destroyed, and when launching it again from the Launcher, I still get the extras!

So, I just tried to remove the extras, in my ACtivity, just after reading them just in case:

Log.e("DEBUG","REMOVE");
getIntent().removeExtra("typeI");
getIntent().putExtra("typeI","");

But that doesn't remove the extras, and next time I open the application Extras are still there.

Any idea what I am doing wrong?

Denysole
  • 3,903
  • 1
  • 20
  • 28
Waza_Be
  • 39,407
  • 49
  • 186
  • 260

2 Answers2

1

When you remove the "extras", this only removes the "extras" in the "in-memory instance of the Intent". The original Intent is still persisted by Android and is reused when you launch the app again.

This is a bug, or a feature, depending on what you are trying to do.

I wrote a rather lengthly response to a similar question in 2013. Have a look at my answer here. You probably want to use Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS.

You also shouldn't need to use launchMode="singleInstance". This special launch mode is not necessary unless you are writing a HOME-screen replacement.


EDIT: You indicated in a comment that this didn't work for you. I think the problem is that you are using your root Activity (the one with ACTION=MAIN and CATEGORY=LAUNCHER) in the Notification. Try this instead:

For the Notification, don't use MainActivity, but NotificationActivity. Define NotificationActivity as an alias for MainActivity in the manifest like this:

<activity-alias
    android:name=".NotificationActivity"
    android:targetActivity=".MainActivity"
    android:label="@string/app_name"
    android:theme="@style/AppTheme.Launcher">
</activity>

This entry should not contain any <intent-filter> and must appear after the manifest entry for MainActivity.

David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • Your answer look awesome, I will try tomorrow, (many chances I will accept your answer as it looks great) and I am really looking forward your long explanation, is there a missing link, or is it just that I don't see it? – Waza_Be Jan 08 '18 at 20:48
  • So sorry, I forgot to link to the answer! I've added the link to the answer and here it is again: https://stackoverflow.com/questions/19813652/remove-data-from-notification-intent/19820057#19820057 – David Wasser Jan 08 '18 at 21:20
  • Were you able to try this? Did it solve your problem? – David Wasser Jan 12 '18 at 18:01
  • Arf, I accepted the answer, but the Intent is still keeping the extras (I removed the singleinstance, and added the flag) – Waza_Be Jan 15 '18 at 08:31
  • notificationIntent = new Intent(getBaseContext(), MainActivity.class); notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);} notificationIntent.putExtra("typeI",data.get("type")); PendingIntent pi = PendingIntent.getActivity(getBaseContext(), 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); – Waza_Be Jan 15 '18 at 08:37
  • I've edited my answer with an update. Have you tried that? – David Wasser Jan 16 '18 at 16:55
0

try this this

getIntent().replaceExtras(new Bundle());
getIntent().setData(null);
after this add you value
getIntent().putExtra("typeI","");
avez raj
  • 2,055
  • 2
  • 22
  • 37
  • replacing the previous bundle with a emplty bunle will erase the data and now can put the new data – avez raj Jan 05 '18 at 13:27
  • Despite the fact that I repaced all the extra, getExtra in onCreate has still some extras even after I exit the app with back! I am completely lost here... – Waza_Be Jan 05 '18 at 15:52
  • This only replaces the "extras" in the "in-memory instance of the `Intent`". Android has persisted the original `Intent` and will reuse that when the user opens the app again. This is no help. – David Wasser Jan 07 '18 at 20:38
  • This code still only updates the "extras" in the "in-memory instance of the `Intent`". This won't help. – David Wasser Jan 16 '18 at 16:56