Fragment的onCreate和onCreateView的区别
来源:学生作业帮助网 编辑:六六作业网 时间:2024/11/26 21:58:43
Fragment的onCreate和onCreateView的区别
Fragment的onCreate和onCreateView的区别
Fragment的onCreate和onCreateView的区别
onCreate是指创建该fragment,类似于Activity.onCreate,你可以在其中初始化除了view之外的东西;
onCreateView是创建该fragment对应的视图,你必须在这里创建自己的视图并返回给调用者.
例如
[java] view plaincopyprint?
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.main,container,false);
mLine1 = (TextView)rootView.findViewById(R.id.line1);
mLine2 = (TextView)rootView.findViewById(R.id.line2);
mDescription1 = (TextView)rootView.findViewById(R.id.line1_description);
mDescription2 = (TextView)rootView.findViewById(R.id.line2_description);
mVersion = (TextView)rootView.findViewById(R.id.version);
mSignWifi = (TextView)rootView.findViewById(R.id.advice);
mSignStreaming = (LinearLayout)rootView.findViewById(R.id.streaming);
mSignInformation = (LinearLayout)rootView.findViewById(R.id.information);
mPulseAnimation = AnimationUtils.loadAnimation(mApplication.getApplicationContext(), R.anim.pulse);
mTextBitrate = (TextView)rootView.findViewById(R.id.bitrate);
return rootView ;
}
部分代码在onCreateView中调用super.onCreateView.
其实有没有调用都无所谓,因为super.onCreateView是直接返回null的.