两个fragment之间传参数方法

来源:互联网 发布:java数据库课程设计 编辑:程序博客网 时间:2024/06/05 17:38

目前正在学习fragment 

fragment之间传参数

第一种: 构造函数传值 这种方法适合传递参数比较少的

public class FirstFragment extends Fragment {    public  FirstFragment(String context){        this.context = context;    }
在fragment中重写构造函数
调用的时候
private TestFangment f1;
f1 = new FirstFragment("第1个Fragment");
第二种:用Bundle和setArguments传参数
想跳转到的目标fragment为 SecondFragment,创建SecondFragment的时候 加入参数即可。
FragmentManager fManager = getFragmentManager( );FragmentTransaction fTransaction = fManager.beginTransaction();
SecondFragment t2 = new SecondFragment();
Bundle bundle = new Bundle();bundle.putString("id","参数");t2.setArguments(bundle);
fTransaction.add(R.id.fragment_container, t2, id);fTransaction.addToBackStack(null);fTransaction.commit();
获取参数 
SecondFragment类中reny任意函数调用getArguments()即可
String string = getArguments().getString("id");  

原创粉丝点击