安卓.无法清除或替换其他内容 [英] Android. Can't clear or replace extras

查看:157
本文介绍了安卓.无法清除或替换其他内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从通知中午餐活动.

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); 

如果活动被破坏,则意图创建新活动,并在oncreate活动中调用此代码

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);
    }
}

如果我通过后退"按钮关闭活动并再次启动活动,应用程序仍会看到,该意图具有额外的键GCMIntentService.GCM_EXTRA_ID (这是一个公共静态字符串).

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

如何清除或替换此多余的部分以供下次启动应用程序使用. getIntent().removeExtra()getIntent().getExtras().clear(),getIntent().putExtra(GCMIntentService.GCM_EXTRA_ID, 0)不起作用.我能做些什么 ?活动具有android:launchMode ="singleTop"键.

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.

推荐答案

此问题通过使用onNewIntent而不是在Activity中使用onCrete来解决.

This problem was solved by using onNewIntent instead of using onCrete in Activity.

这篇关于安卓.无法清除或替换其他内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆