情形一:启动应用加载Activity和Fragment
Activity::onCreate
Fragment::onAttach
Fragment::onCreate
Fragment::onCreateView
Fragment::onActivityCreate
Activity::onStart
Fragment::onSatrt
Activity::onResume
Fragment::onResume
情形二:fragA->fragB->fragA(Fragment不加入回退栈)
销毁FragmentA:
启动FragmentB:
销毁FragmentB:
启动FragmentA:
启动过程如下
onAttach
onCreate
onCreateView
onActivityCreate
onSatrt
onResume
销毁过程如下:
onPause
onStop
onDestroyView
onDestroy
onDetach
在不加入回退栈的情况下,无论多少Fragment之间的切换,都遵循启动下一个,销毁上一个的原则,在屏幕不旋转的情况下Activity是不会发生任何变化的
情形三:旋转Fragment(与加不加入回退栈没关系,过程都一样)
Activity和Fragment都会被销毁,其中销毁过程Fragment会保存状态
Fragment::onPause
Activity::onPause
Fragment::onSaveInstanceState(保存销毁前的状态)
Fragment::onStop
Activity onStop
Fragment::onDestroyView
Fragment::onDestroy
Fragment::onDetach
Activity::onDestroy
创建Activity和Fragment(与一开始启动应用过程相同)
Activity::onCreate
Fragment::onAttach
Fragment::onCreate
Fragment::onCreateView
Fragment::onActivityCreated
Fragment::onAttach
Fragment::onCreate
Fragment::onCreateView
Fragment::onActivityCreated
Activity onStart
Fragment::onStart
Fragment::onStart
Activity::onResume
Fragment::onResume
Fragment::onResume
情形四:FragmentA->FragmentB->FragmentA(两者都加入回退栈)
停止FragmentA,但是不销毁,与不加入回退栈相比唯一的不同就是不会被销毁
FragmentA::onPause
FragmentA::onStop
FragmentA::onDestroyView
启动FragmentB
"恢复"FragmentA,这种恢复会从Fragment的onCreateView开始,重新绘制页面,所以之前的所有数据都已经不在了,这就涉及到Fragment的状态保存(下节待续)
onCreateView
onActivityCreated
onStart
onResume