Activity
- 进入页面
onCreate - onStatrt - onResume — onRestart - onPause - onStop - onDestroy
Fragment
-
创建 onAttach - onCreate - onCreateView - onViewCreated - onActivityCreated - onStart - onResume -
销毁 onPause - onStop - onDestroyView - onDestroy - onDetach -
home键/熄屏/进入任务页 onPause - onSaveInstanceState - onStop -
home后重进/亮屏/任务页重进 onStart - onResume
ViewPager2中的Fragment的生命周期
前提: 3个Fragment, setOffscreenPageLimit(x) Fragment会比x预创建一个
- 进入页面(x1)
2022-02-23 11:29:36.729 A/(AFragment.java:52)(onAttach): ---> "1"
2022-02-23 11:29:36.730 A/(AFragment.java:58)(onCreate): ---> "2"
2022-02-23 11:29:36.731 A/(AFragment.java:63)(onCreateView): ---> "3"
2022-02-23 11:29:36.734 A/(AFragment.java:70)(onViewCreated): ---> "4"
2022-02-23 11:29:36.758 A/(AFragment.java:76)(onActivityCreated): ---> "5"
2022-02-23 11:29:36.759 A/(AFragment.java:82)(onViewStateRestored): ---> "6"
2022-02-23 11:29:36.760 A/(AFragment.java:88)(onStart): ---> "7"
2022-02-23 11:29:36.776 A/(AFragment.java:94)(onResume): ---> "8"
2022-02-23 11:29:36.779 E/(BFragment.java:47)(onAttach): ---> "1"
2022-02-23 11:29:36.780 E/(BFragment.java:53)(onCreate): ---> "2"
2022-02-23 11:29:36.781 E/(BFragment.java:58)(onCreateView): ---> "3"
2022-02-23 11:29:36.784 E/(BFragment.java:65)(onViewCreated): ---> "4"
2022-02-23 11:29:36.800 E/(BFragment.java:71)(onActivityCreated): ---> "5"
2022-02-23 11:29:36.801 E/(BFragment.java:77)(onViewStateRestored): ---> "6"
2022-02-23 11:29:36.803 E/(BFragment.java:83)(onStart): ---> "7"
- 从A滑到BFragment(此时先创建C,再执行AB的生命周期)
W/(CFragment.java:46)(onAttach): ---> "1"
W/(CFragment.java:52)(onCreate): ---> "2"
W/(CFragment.java:57)(onCreateView): ---> "3"
W/(CFragment.java:64)(onViewCreated): ---> "4"
W/(CFragment.java:70)(onActivityCreated): ---> "5"
W/(CFragment.java:76)(onViewStateRestored): ---> "6"
W/(CFragment.java:82)(onStart): ---> "7"
A/(AFragment.java:101)(onPause): ---> "-1"
E/(BFragment.java:89)(onResume): ---> "8"
- 在2的基础上(从B退出), 退出页面
当前的Fragment会pause,其他的按照ABC顺序执行E/(BFragment.java:96)(onPause): ---> "-1"
A/(AFragment.java:107)(onStop): ---> "-2"
E/(BFragment.java:102)(onStop): ---> "-2"
W/(CFragment.java:101)(onStop): ---> "-2"
A/(AFragment.java:113)(onDestroyView): ---> "-3"
A/(AFragment.java:119)(onDestroy): ---> "-4"
A/(AFragment.java:125)(onDetach): ---> "-5"
E/(BFragment.java:108)(onDestroyView): ---> "-3"
E/(BFragment.java:114)(onDestroy): ---> "-4"
E/(BFragment.java:120)(onDetach): ---> "-5"
W/(CFragment.java:107)(onDestroyView): ---> "-3"
W/(CFragment.java:113)(onDestroy): ---> "-4"
W/(CFragment.java:119)(onDetach): ---> "-5"
|