Android. Can't clear or replace extras

牧云@^-^@ 提交于 2019-12-24 16:35:25

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!