【问题标题】:Change intent bundle data before an activity is re-created after orientation change在方向更改后重新创建活动之前更改意图包数据
【发布时间】:2010-11-15 10:32:20
【问题描述】:

我有一个通知,它启动我的活动并使用意图的 putExtra() 函数传递消息。然后该消息在活动的 onCreate 函数中显示给用户。当应用程序由于方向改变而重新启动时,消息会再次显示,因为它仍然在 Intent 的捆绑数据中。

如何删除多余的数据?

我尝试了以下方法:

Bundle bundle = getIntent().getExtras();
if (bundle.getBoolean("showMessage")) {
    // ... show message that is in bundle.getString("message")
    // remove message
    bundle.remove("showMessage");
}

但是仍然显示在方向改变后仍然显示,似乎使用的意图不是我改变的意图,而是原来的。我发现的唯一解决方法是将 showMessage 额外保存在 onSaveInstanceState() 中。 还有其他方法吗?或者这是要走的路?

【问题讨论】:

    标签: android android-intent bundle


    【解决方案1】:

    (简单的)解决方案是:

    而不是调用

    bundle.remove("showMessage");
    

    我现在用

    getIntent().removeExtra("showMessage");
    

    按预期工作。似乎 getIntent().getExtras() 返回一个副本,而不是引用。

    【讨论】:

    • 对不起,-1。 @CommonsWare 指出,onSaveInstanceState 是正确的做法。
    【解决方案2】:

    您的onSaveInstanceState() 方法是正确的答案,AFAIK。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-05
      • 2017-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多