使用Fragment的时候可能需要在两个Fragment之间进行参数的传递,找到一个方法就能实现像Activity一样便捷的实现参数传递 程序中的一段代码
Restart sf = new Restart(this.getActivity(),w,h);
Bundle bundle = new Bundle();
bundle.putString("second",second+"" ); //second 为一个数字,将它变成string
sf.setArguments(bundle);
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.container, sf);
transaction.commit();
可以使用bundle进行参数传递,这样在两个Fragment跳转的时候就可以带上参数了,在另外一个Fragment获取参数的方式只需要一个语句
String second = getArguments().getString("second");
String count = getArguments().getString("count");
TextView tv=(TextView)view.findViewById(R.id.score);
tv.setText(second+":"+(100-Integer.parseInt(count)))
second和count是自己定义的一个标识,参数的形式只要bundle能传递都可以实现