参考自 http://blog.csdn.net/flowingflying/article/details/12505453 系列八篇文章
很详细,在这总结一些自己需要的
一,关于生命周期
因为Fragment有两种引入方式,两种引入方式生命周期不尽相同(这里主要比较的是开启Fragment时的不同)
1,由布局文件xml中指定类名称而引入Fragment
生命周期为:
activity:oncreate->setContentView-> ->setContentView->oncreate
fragment: onInflate->onAttach->onCreate->onCreateView ->onActivityCreate->onStart->onResume
注:setContentView为oncreate中的,首尾的setContentView和oncreate分别代表其开始和结束
2,另外一种是在Fragment类中代码引入Fragment
生命周期为:
fragment:onAttach-->onCreate-->onCreateView-->onActivityCreate-->onStart-->onResume
不同之处:1,引入方式2中就没有onInflate方法了
2,引入方式2的fragment生命周期与activity没有关系了
3,引入方式1的setContentView的开始和结束,oncreate的结束分别为fragment生命周期的三个切割点
从程序的运行输出也可以看出activity和fragment的生命周期了