问题
I lunch activity from notification.
Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP );
notificationIntent.putExtra(GCM_EXTRA_ID, id);
PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_SOUND;
notificationManager.notify(0, notification);
If activity destroyed, intent creates new activity and call in oncreate activity this code
Bundle extras = getIntent().getExtras();
if(extras != null){
if(extras.containsKey(GCMIntentService.GCM_EXTRA_ID))
{
int id = extras.getInt(GCMIntentService.GCM_EXTRA_ID);
Toast.makeText(this, "GCM_EXTRA_ID ACTIVITY ON CREATE:" + Integer.toString(id) , Toast.LENGTH_LONG).show();
dbClassItem = new DBClass(this);
db = dbClassItem.getWritableDatabase();
DBClass.setLocateValue(db, id);
db.close();
getIntent().putExtra(GCMIntentService.GCM_EXTRA_ID, 0);
}
}
If I close activity by back button and start activity again application still see, that intent has extra with key GCMIntentService.GCM_EXTRA_ID
(this is a public static string).
How can I clear or replace this extra for next app start. getIntent().removeExtra()
, getIntent().getExtras().clear(),getIntent().putExtra(GCMIntentService.GCM_EXTRA_ID, 0)
does not work. What can I do ? Activity has android:launchMode ="singleTop"
key.
回答1:
This problem was solved by using onNewIntent instead of using onCrete in Activity.
回答2:
You can do this
notificationIntent.putExtra(GCM_EXTRA_ID, null);
来源:https://stackoverflow.com/questions/20766249/android-cant-clear-or-replace-extras