上篇文章讲到了activity四种模式下调整时的生命周期,这篇文章我们聊聊activity和fragment混合使用时的两者的生命周期
先看下面对比图:
1. activity启动时顺便加载里面的fragment,这种情况下的生命周期如下:
- onCreate过程
- Activity &&&& onCreate...
- Fragment **** onAttach...
- Fragment **** onCreate...
- Fragment **** onCreateView...
- Fragment **** onActivityCreated...
- onStart过程
- Activity &&&& onStart...
- Fragment **** onStart...
- onResume过程
- Activity &&&& onResume...
- Fragment **** onResume...
- onPause过程
- Fragment **** onPause...
- Activity &&&& onPause...
- onStop过程
- Fragment **** onStop...
- Activity &&&& onStop...
- onStart过程
- Activity &&&& onStart...
- Fragment **** onStart...
- onResume过程
- Activity &&&& onResume...
- Fragment **** onResume...
- onPause过程
- Fragment **** onPause...
- Activity &&&& onPause...
- onStop过程
- Fragment **** onStop...
- Activity &&&& onStop...
- onDestroy过程
- Fragment **** onDestroyView...
- Fragment **** onDestroy...
- Fragment **** onDetach...
- Activity &&&& onDestroy...
2. activity A启动时顺便加载里面的fragment A,点击按钮跳转到activity B启动时顺便加载里面的fragment B,这种情况下的生命周期如下:
- onCreate过程 ActivityA和FragmentA创建
- ActivityA &&&& onCreate...
- FragmentA **** onAttach...
- FragmentA **** onCreate...
- FragmentA **** onCreateView...
- FragmentA **** onActivityCreated...
- onStart过程
- ActivityA &&&& onStart...
- FragmentA **** onStart...
- onResume过程
- ActivityA &&&& onResume...
- FragmentA **** onResume...
-
- 点击按钮ActivityA替换成ActivityB
- FragmentA **** onPause...
- ActivityA &&&& onPause...
-
- ActivityB &&&& onCreate...
- FragmentB **** onAttach...
- FragmentB **** onCreate...
- FragmentB **** onCreateView...
- FragmentB **** onActivityCreated...
-
- ActivityB **** onStart...
- FragmentB **** onStart...
-
- ActivityB **** onResume...
- FragmentB **** onResume...
- FragmentA **** onStop...
- ActivityA **** onStop...
- 按下返回键
- onPause过程
- FragmentB **** onPause...
- ActivityB &&&& onPause...
- ActivityA **** onStart...
- FragmentA **** onStart...
- ActivityA **** onResume...
- FragmentA **** onResume...
-
- FragmentB **** onStop...
- ActivityB **** onStop...
- onDestroy过程
- FragmentB **** onDestroyView...
- FragmentB **** onDestroy...
- FragmentB **** onDetach...
- ActivityA &&&& onDestroy...
3.activity A启动时顺便加载里面的fragment A,fragment A更换成fragment B,这种情况下的生命周期如下:
- onCreate过程 ActivityA和FragmentA创建
- ActivityA &&&& onCreate...
- FragmentA **** onAttach...
- FragmentA **** onCreate...
- FragmentA **** onCreateView...
- FragmentA **** onActivityCreated...
- onStart过程
- ActivityA &&&& onStart...
- FragmentA **** onStart...
- onResume过程
- ActivityA &&&& onResume...
- FragmentA **** onResume...
-
- 点击按钮fragmentA替换成fragmentB
- 右边为FragmentTransaction.addToBackStack
- FragmentA **** onPause... FragmentA **** onPause...
- FragmentA **** onStop... FragmentA **** onStop...
- FragmentA **** onDestroyView... FragmentA **** onDestroyView...
- FragmentA **** onDestroy...
- FragmentA **** onDetach...
-
- FragmentB **** onAttach... FragmentB **** onAttach...
- FragmentB **** onCreate... FragmentB **** onCreate...
- FragmentB **** onCreateView... FragmentB **** onCreateView...
- FragmentB **** onActivityCreated... FragmentB **** onActivityCreated...
- FragmentB **** onStart... FragmentB **** onStart...
- FragmentB **** onResume... FragmentB **** onResume...
- 按下返回键 FragmentA **** onDestroy...
- FragmentA **** onDetach...
-
- onPause过程
- FragmentB **** onPause...
- ActivityA &&&& onPause...
- onStop过程
- FragmentB **** onStop...
- ActivityA &&&& onStop...
- onDestroy过程
- FragmentB **** onDestroyView...
- FragmentB **** onDestroy...
- FragmentB **** onDetach...
- ActivityA &&&& onDestroy...
- 今晚总结到这里,哈哈,大家感兴趣也可以试试不同场景下的跳转activity和fragment的生命周期,经过这两节的讲解,面试问到activity和fragment的生命周期大概能够秒杀了。