如何将所有映射从Bundle传输到另一个包而不覆盖它?

时间:2018-06-04 23:34:07

标签: java android android-intent android-activity bundle

我试过这个,但它会覆盖现有的捆绑包:

Bundle b1 = new Bundle();
b1.putString("name", "Abraham");
Intent i = getIntent();
Bundle b2 = i.getExtras();
b1.putAll(b2);

我失去了亚伯拉罕...

1 个答案:

答案 0 :(得分:1)

你在这里做的是: //创建一个新的Bundle

 Bundle b1 = new Bundle();

//在该捆绑包中加入一些值

b1.putString("name", "Abraham");

//创建新的意图

Intent i = getIntent();

并且没有像这样将

<强> i.putExtras(B1);

//这里你从意图中得到一个空包。

Bundle b2 = i.getExtras();

//所以你没有得到任何捆绑,而是获得异常

b1.putAll(b2);

您只需将捆绑包分配给意图。如上面粗体提到的那样。