如何将一些数据传输到另一个片段?如何将一些数据传输到另一个Fragment同样地,它也是用extras为intents?
3 回答
扬帆大鱼
TA贡献1799条经验 获得超9个赞
Bundle
Fragment fragment = new Fragment();Bundle bundle = new Bundle();bundle.putInt(key, value);fragment.setArguments(bundle);
FragmentonCreate()
Bundle bundle = this.getArguments();if (bundle != null) {
int myInt = bundle.getInt(key, defaultValue);}
慕仙森
TA贡献1827条经验 获得超8个赞
public class MyClass implements Serializable {
private static final long serialVersionUID = -2163051469151804394L;
private int id;
private String created;}Bundle args = new Bundle();args.putSerializable(TAG_MY_CLASS, myClass);Fragment toFragment = new ToFragment(); toFragment.setArguments(args);getFragmentManager() .beginTransaction() .replace(R.id.body, toFragment, TAG_TO_FRAGMENT) .addToBackStack(TAG_TO_FRAGMENT).commit();
@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Bundle args = getArguments();
MyClass myClass = (MyClass) args .getSerializable(TAG_MY_CLASS);
慕勒3428872
TA贡献1848条经验 获得超6个赞
使用片段到片段传递数据的完整代码
Fragment fragment = new Fragment(); // replace your custom fragment class Bundle bundle = new Bundle();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
bundle.putString("key","value"); // use as per your need
fragment.setArguments(bundle);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.replace(viewID,fragment);
fragmentTransaction.commit();在自定义片段类中
Bundle mBundle = new Bundle();mBundle = getArguments();mBundle.getString(key); // key must be same which was given in first fragment
- 3 回答
- 0 关注
- 482 浏览
添加回答
举报
0/150
提交
取消
