将多个包传递给片段[Android]

将多个包传递给片段[Android],android,fragment,bundle,Android,Fragment,Bundle,在传递到Android上的片段之前,是否可以将两个捆绑包合并为一个? 所以代码片段类似于: Bundle b1 = SomeClass1.getSomeBundle(); Bundle b2 = SomeClass2.getDifferentBundle(); // How to do I pass these two bundles to a fragment? fragment.setArgument(b1 + b2); // Illustrative only. 是否可以

在传递到Android上的片段之前,是否可以将两个捆绑包合并为一个? 所以代码片段类似于:

  Bundle b1 = SomeClass1.getSomeBundle();
  Bundle b2 = SomeClass2.getDifferentBundle();
  // How to do I pass these two bundles to a fragment?
  fragment.setArgument(b1 + b2); // Illustrative only.
是否可以在传递给客户之前将两个捆绑组合成一个 Android上的碎片

是的,你可以使用

或者,您可以通过使用创建捆绑包来分别传递这两个捆绑包

是否可以在传递给客户之前将两个捆绑组合成一个 Android上的碎片

是的,你可以使用

或者,您可以通过使用Bundle can-contains Bundle创建一个Bundle来分别传递这两个Bundle。你可以用

要检索它,可以使用

Bundle也可以包含Bundle。你可以用

要检索它,可以使用


Bundle可以包含Bundle

Bundle mainBundle = new Bundle();
Bundle b1 = SomeClass1.getSomeBundle();
Bundle b2 = SomeClass2.getDifferentBundle();

mainBundle.putBundle("b1", b1);
mainBundle.putBundle("b2", b2);

fragment.setArgument(mainBundle);
要从捆绑包中获取捆绑包,只需调用:

Bundle b1 = mainBundle.getBundle("b1");
Bundle b2 = mainBundle.getBundle("b2");

Bundle可以包含Bundle

Bundle mainBundle = new Bundle();
Bundle b1 = SomeClass1.getSomeBundle();
Bundle b2 = SomeClass2.getDifferentBundle();

mainBundle.putBundle("b1", b1);
mainBundle.putBundle("b2", b2);

fragment.setArgument(mainBundle);
要从捆绑包中获取捆绑包,只需调用:

Bundle b1 = mainBundle.getBundle("b1");
Bundle b2 = mainBundle.getBundle("b2");

为什么有两个不同的包?而不是一个为什么有两个不同的捆绑?我读到putAll将覆盖现有的Bundle,而不是one。但不管怎样,putBundle似乎是更好的方式@BarthyB.:是的,如果两个包中有不同的键,那么使用putAll其他明智的使用putBundleDoes putAll具有与只有一个包相同的效果?或者我需要记住我传递了多个bundle。@user3079275:请解释更多我有两个具有不同键的bundle,所以我认为putAll的效果就像我传递了bundle1+bundle2的bundle,对吗?如果是这样的话,我需要使用putAll我已经读到putAll将覆盖现有的Bundle。但不管怎样,putBundle似乎是更好的方式@BarthyB.:是的,如果两个包中有不同的键,那么使用putAll其他明智的使用putBundleDoes putAll具有与只有一个包相同的效果?或者我需要记住我传递了多个bundle。@user3079275:请解释更多我有两个具有不同键的bundle,所以我认为putAll的效果就像我传递了bundle1+bundle2的bundle,对吗?如果是这样,我需要使用putAll