意图演员保持不变,即使当更新 [英] Intent extras remain the same even when updated

查看:118
本文介绍了意图演员保持不变,即使当更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过活动使用实例的意图与附加功能。

这似乎每当我使用操作栏中的后退按钮或导航它们之间导航做工精细。但是,如果我访问主屏幕,然后重新启动应用程序,通过了被忽略的演员;第二个活动似乎使用旧的意图,而不是新的。

相关code:

源活动

 公共类ActivityA延伸活动{
    保护无效goToResults(字符串结果){
        意向意图=新意图(这一点,ActivityB.class);
        intent.putExtra(Intent.EXTRA_TEXT,结果);
        startActivity(意向);
    }
}

目标活动

 公共类ActivityB延伸活动{
    @覆盖
    公共无效onResume(){
        super.onResume();
        捆绑额外= getIntent()getExtras()。
        字符串结果= extras.getString(Intent.EXTRA_TEXT);
        //等
    }
 }

我已经尝试了一些不同的东西,其中包括:

  intent.setAction(行动 - + UNIQUE_ID);

(因为据我所知,意图实例不受额外的内容相比)

  PendingIntent.getActivity(这一点,0,意向,PendingIntent.FLAG_UPDATE_CURRENT);

(我不需要的PendingIntent ,但认为这可能迫使意图更新)

有关如何强制任何建议在意图来我每次做从 ActivityA 的过渡时间显示更改的数据 - > ActivityB ,无论我是否使用后退按钮或分流到主屏幕


解决方案

我记得运行到这个问题,一旦我们解决了通过添加Intent.FLAG_ACTIVITY_CLEAR_TOP给您发送的意图:

  intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

或实施以下方法进入活动你的意图推出:

 保护无效onNewIntent(意向意图){
        super.onNewIntent(意向);
        this.setIntent(意向);
}

我不是100%肯定又是什么固定它,相信它被添加onNewIntent方法。
祝你好运,让我们知道。

I am trying to pass a small bit of text between Activity instances using an Intent with extras.

This seems to work fine whenever I navigate between them using the back button or navigation in the action bar. However, if I visit the home screen and then relaunch the application, the extras passed are ignored; the second Activity seems to use the old Intent, rather than the new one.

The relevant code:

Source activity

public class ActivityA extends Activity {
    protected void goToResults(String results) {
        Intent intent = new Intent(this, ActivityB.class);
        intent.putExtra(Intent.EXTRA_TEXT, results);
        startActivity(intent);
    }
}

Destination activity

public class ActivityB extends Activity {
    @Override
    public void onResume() {
        super.onResume();
        Bundle extras = getIntent().getExtras();
        String results = extras.getString(Intent.EXTRA_TEXT);
        // etc
    }
 }

I have tried a number of different things, including:

intent.setAction("action-" + UNIQUE_ID);

(as I understand that Intent instances are not compared by content of extras)

PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

(I don't need PendingIntent, but thought this might force the Intent to update)

Any suggestions for how to force the Intent to show the changed data every time I make the transition from ActivityA -> ActivityB, regardless of whether I'm using the back button or a diversion to the home screen?

解决方案

I remember running into this issue once, we solved it by either adding Intent.FLAG_ACTIVITY_CLEAR_TOP to the intent you are sending:

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

or by implementing the following method into the activity you're intent is launching:

protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        this.setIntent(intent);
}

I'm not 100% sure what fixed it again, believe it was adding the onNewIntent method. Good luck and let us know.

这篇关于意图演员保持不变,即使当更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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