首页 > 代码库 > Fragment的总结:Fragment的生命周期及与Activity生命周期的对比
Fragment的总结:Fragment的生命周期及与Activity生命周期的对比
1.Fragment的生命周期:
先看Google帮助文档中给出的图示:
//附加Fragment到当前Activity上.
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);
}
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
// 通过inflater对象将布局文件转换成View对象
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return super.onCreateView(inflater, container, savedInstanceState);
}
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
}
2.通过打印日志方式对比Activity生命周期:
截图一:
截图一说明Fragmeng创建时调用的前四个方法对应Activity中的onCreate()方法.
假如当有电话打进,Fragment不在前台显示时:
当返回到Fragment页面时:
当退出Fragment页面时:
Fragment的总结:Fragment的生命周期及与Activity生命周期的对比