经常看到fragment是new出来的,且参数都放在构造方法里面传递,虽然不报错,但总感觉不对劲。
正确方法:
public static MatcherRecordFragment newInstance(int param1) {
MatcherRecordFragment fragment = new MatcherRecordFragment();
Bundle args = new Bundle();
args.putInt(ARG_PARAM1, param1);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = getActivity();
if( null != getArguments()){
state = getArguments().getInt(ARG_PARAM1)+1;
}
}
本文介绍了Android开发中Fragment的正确创建方式,通过newInstance静态方法配合Bundle传递参数,并在onCreate中获取这些参数。

1485

被折叠的 条评论
为什么被折叠?



