如何使用一些微调器的值设置从不同的片段打开片段?

时间:2017-07-17 14:31:35

标签: java android android-fragments android-spinner

我有两个片段。一个片段包含饼图和一些按钮。在单击饼图或按钮时,将打开第二个片段,其中包含一些微调器。现在,第二个片段的内容由那些微调器呈现。这些微调器的值取决于单击的按钮。如何将这些信息发送给那些微调器?要转移的信息是对象的形式。

2 个答案:

答案 0 :(得分:1)

  1. Use Bundles for passing arguments for your Fragment. Never use setters/getters, because those could not correspond with your fragment's lifecycle. Create public constants in the new fragment in order to use them as keys in your Bundle hashmap.

  2. Depending on your layouts hierarchy, use getActivity().getSupportFragmentManager() (in case you want the new fragment to be opened on the same 'level' of hierarchy as the previous one) or getChildFragmentManager() (if you want the new fragment to be inside of the previous one).

Hope this helps!

答案 1 :(得分:0)

如果你想传递一个模型对象,那么两个活动/片段会按照惯例创建你的POJO。 您可以通过转到Android Studio>快速将此类设为parcelable。偏好>插件>搜索parcelable并安装Android Parcelable代码生成器。重启Android工作室。然后代码>生成> Parcelable将在您的班级上实施所有必要的方法。您应该可以执行以下操作:

Bunlde bundle = new Bundle();
bundle.putParcelable("key", YourPojoWhichHasImplimentedParcelable);