Android Intent Extras随处可见 [英] Android Intent Extras Sticking Around

查看:105
本文介绍了Android Intent Extras随处可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Heelo,我有一个活动,单击按钮时会打开gridLayout.返回到mainactivity时,此gridLayout发送一些意向附加项.这很好,我收到了附加项 我的问题是,如果我单击主页"按钮或应用程序转到 onPause()并返回此意向附加信息,则再次发送该信息.如何删除它们?

Heelo , I am having an activity that opens a gridLayout upon button click.This gridLayout sends some intent extras when returning back to mainactivity.This works fine i receive the extras My issue is that if i click the home button or app goes to onPause() and returns back this intent extras are sent again with it. How can i remove them?

   try {
        name = getIntent().getExtras().getString("name");

        contact = getIntent().getExtras().getString("contact");
        tv.setText(name);
        file = getIntent().getExtras().getString("file");
        Boolean fromgrid = getIntent().getExtras().getBoolean("fromgrid");
        getIntent().removeExtra("file");
        Log.w("chat2file", file + fromgrid);

    } catch (NullPointerException e) {
        e.printStackTrace();
        file = null;
    }



@Override
protected void onPause() {

    super.onPause();
    file = null;
    SharedPreferences.Editor edit = sp.edit();
    edit.putString("runservice", "yes");
    edit.commit();
    getIntent().removeExtra("file");
    runthread = false;

}

在gridview中

            i.putExtra("file", imageUrls[position]);
            i.putExtra("name", name);
            i.putExtra("contact", contact);
            i.putExtra("fromgrid", true);
            //i.setAction("actionstring" + System.currentTimeMillis());

            startActivity(i);

推荐答案

尽可能调用Intent.removeExtra(String),其中String是键,例如文件",名称"等.

Whenever possible, call Intent.removeExtra(String), where String is the key, such as "file", "name", etc.

此外,调用getIntent()会返回启动您的活动的意图.这可能不是您想要的...如果通过广播接收器发送带有附加功能的意图,则需要指定该意图.请注意,您要从哪些意图中删除多余的内容.

Also, calling getIntent() returns the intent that started your activity. This may not be what you are looking for...if the intent with the extras is sent via BroadcastReceiver, you want to specify THAT intent. Be careful which intents you are removing extras from.

这篇关于Android Intent Extras随处可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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