传递数据给Fragment的方法

来源:互联网 发布:矢量图制作知乎 编辑:程序博客网 时间:2024/05/30 07:13

原文:http://stackoverflow.com/questions/7149802/how-to-transfer-some-data-to-another-fragment

Use a Bundle. Here's an example:

Fragment fragment = new Fragment();Bundle bundle = new Bundle();bundle.putInt(key, value);fragment.setArguments(bundle);

Bundle has put methods for lots of data types. Seehttp://developer.android.com/reference/android/os/Bundle.html

Then in your Fragment, retrieve the data (e.g. in onCreate()) with:

Bundle bundle = this.getArguments();
if(bundle!=null){
    int myInt = getArguments().getInt(key, defaultValue);
}