在执行一些动作时需要传递变量到 MyFragment,
解决方案:使用newInstance
Fragment myFragment = MyFragment.newInstance(1);
解决方案:使用newInstance
public class MyFragment extends Fragment {
public static MyFragment newInstance(int num) {
MyFragment f = new MyFragment();
// Supply num input as an argument.
Bundle args = new Bundle();
args.putInt("num", num);
f.setArguments(args);
return f;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mNum = getArguments().getInt("num");
...
}
...
}
Fragment myFragment = MyFragment.newInstance(1);