您的位置:首页 > 其它

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

2014-06-27 13:12 441 查看
首先我们需要在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

点击打开链接
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: