深入分析:Fragment与Activity交互的几种方式(二,使用Bundle)

首先我们需要在Activity中动态添加Fragment时,用Bundle封装我们需要传递的数据。

public void button(View view) {
ArgFragment arg = new ArgFragment();
Bundle bundle = new Bundle();
bundle.putString("arg", edit.getText().toString());
arg.setArguments(bundle);
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.layout_fragment, arg);
ft.commit();
}

然后在Fragment的回调函数中通过Fragment提供的方法getArguments()取出Bundle对象。

text = (TextView) view.findViewById(R.id.text);
Bundle bundle = getArguments();
text.setText(bundle.getString("arg"));


针对本文也写了一个Demo,下载地址:http://download.csdn.net/detail/huangyabin001/7560031

点击打开链接

原文地址:https://www.cnblogs.com/bill-technology/p/4130862.html