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?
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
.