1、Fragment的生命周期函数
onAttach、onCreate、onCreateView、onViewCreated、onActivityCreated、onStart、onResume、onPause、onStop、onDestroyView、onDestroy、onDettach
Activity的各种生命周期函数先执行,然后各个Fragment不论是不是可见与否紧接着调用同样的生命周期函数。比如Activity的onResume,几个Fragment的onResume紧跟着执行。
2、Activity和Fragment的生命周期函数的顺序如下:
05-25 13:49:42.757 7242-7242/com.yunzhiyuan100.wish D/Linghu MainActivity.onCreate: ============MainActivity===onCreate
05-25 13:49:42.760 7242-7242/com.yunzhiyuan100.wish D/Linghu MainActivity.onStart: ============MainActivity===onStart
05-25 13:49:42.762 7242-7242/com.yunzhiyuan100.wish D/Linghu MainActivity.onResume: ============MainActivity===onResume
05-25 13:49:42.785 7242-7242/com.yunzhiyuan100.wish D/Linghu HomeFragment.onAttach: ===================HomeFragment===onAttach
05-25 13:49:42.786 7242-7242/com.yunzhiyuan100.wish D/Linghu HomeFragment.onCreate: ===================HomeFragment===onCreate
05-25 13:49:42.786 7242-7242/com.yunzhiyuan100.wish D/Linghu FindFragment.onAttach: ===================FindFragment===onAttach
05-25 13:49:42.787 7242-7242/com.yunzhiyuan100.wish D/Linghu FindFragment.onCreate: ===================FindFragment===onCreate
05-25 13:49:42.787 7242-7242/com.yunzhiyuan100.wish D/Linghu MineFragment.onAttach: ===================MineFragment===onAttach
05-25 13:49:42.787 7242-7242/com.yunzhiyuan100.wish D/Linghu MineFragment.onCreate: ===================MineFragment===onCreate
05-25 13:49:42.788 7242-7242/com.yunzhiyuan100.wish D/Linghu HomeFragment.onCreateView: ===================HomeFragment===onCreateView
05-25 13:49:42.908 7242-7242/com.yunzhiyuan100.wish D/Linghu HomeFragment.onViewCreated: ===================HomeFragment===onViewCreated
05-25 13:49:42.908 7242-7242/com.yunzhiyuan100.wish D/Linghu HomeFragment.onActivityCreated: ===================HomeFragment===onActivityCreated
05-25 13:49:42.920 7242-7242/com.yunzhiyuan100.wish D/Linghu HomeFragment.onStart: ===================HomeFragment===onStart
05-25 13:49:42.920 7242-7242/com.yunzhiyuan100.wish D/Linghu HomeFragment.onResume: ===================HomeFragment===onResume
05-25 13:49:42.921 7242-7242/com.yunzhiyuan100.wish D/Linghu FindFragment.onCreateView: ===================FindFragment===onCreateView
05-25 13:49:42.955 7242-7242/com.yunzhiyuan100.wish D/Linghu FindFragment.onViewCreated: ===================FindFragment===onViewCreated
05-25 13:49:42.956 7242-7242/com.yunzhiyuan100.wish D/Linghu FindFragment.onActivityCreated: ===================FindFragment===onActivityCreated
05-25 13:49:42.956 7242-7242/com.yunzhiyuan100.wish D/Linghu MineFragment.onCreateView: ===================MineFragment===onCreateView
05-25 13:49:43.004 7242-7242/com.yunzhiyuan100.wish D/Linghu MineFragment.onViewCreated: ===================MineFragment===onViewCreated
05-25 13:49:43.004 7242-7242/com.yunzhiyuan100.wish D/Linghu MineFragment.onActivityCreated: ===================MineFragment===onActivityCreated
05-25 13:49:43.005 7242-7242/com.yunzhiyuan100.wish D/Linghu FindFragment.onStart: ===================FindFragment===onStart
05-25 13:49:43.005 7242-7242/com.yunzhiyuan100.wish D/Linghu FindFragment.onResume: ===================FindFragment===onResume
05-25 13:49:43.006 7242-7242/com.yunzhiyuan100.wish D/Linghu MineFragment.onStart: ===================MineFragment===onStart
05-25 13:49:43.006 7242-7242/com.yunzhiyuan100.wish D/Linghu MineFragment.onResume: ===================MineFragment===onResume
MainActivity onCreate-----onStart-----onResume---------》HomeFragment onAttach------onCreate
FindFragment onAttach------onCreate
MineFragment onAttach------onCreate
HomeFragment onCreateView------onViewCreated-------onActivityCreeated---onStart-----onResume
FindFragment onCreateView------onViewCreated--------onActivityCreated
MineFragment onCreateView-------onViewCreated-------onActivityCreated
FindFragment onStart-------onResume
MineFragment onStart-------onResume
Activity 生命周期走到可见,然后依次执行Fragment的onAttach----onCreate, 然后依次onCreateView、onViewCreated、onActivityCreated,第一个Fragment的onstart、onResume一起执行了,然后最后是后两个Fragment的onStart、onResume。
2、下面是加入了setUserVisibleHint
Activity和Fragment的生命周期函数的顺序如下:
MainActivity onCreate-----onStart-----onResume-------------》 HomeFragment setUserVisibleHint(false)
FindFragment setUserVisibleHint(false)
MineFragment setUserVisibleHint(false)
HomeFragment setUserVisibleHint(true)
HomeFragment onAttach------onCreate
FindFragment onAttach------onCreate
MineFragment onAttach------onCreate
HomeFragment onCreateView------onViewCreated-------onActivityCreeated---onStart-----onResume
FindFragment onCreateView------onViewCreated--------onActivityCreated
MineFragment onCreateView-------onViewCreated-------onActivityCreated
FindFragment onStart-------onResume
MineFragment onStart-------onResume
切换tab,点击FindFagment
HomeFragment setUserVisibleHint(false)
FindFragment setUserVisibleHint(true)
切换tab,点击MineFragment
FindFragment===setUserVisibleHint(false)
MineFragment===setUserVisibleHint(true)
log如下:
05-25 14:11:09.453 8685-8685/com.yunzhiyuan100.wish D/Linghu MainActivity.onCreate: ============MainActivity===onCreate
05-25 14:11:09.454 8685-8685/com.yunzhiyuan100.wish D/Linghu MainActivity.onStart: ============MainActivity===onStart
05-25 14:11:09.456 8685-8685/com.yunzhiyuan100.wish D/Linghu MainActivity.onResume: ============MainActivity===onResume
05-25 14:11:09.481 8685-8685/com.yunzhiyuan100.wish D/Linghu HomeFragment.setUserVisibleHint: ===================HomeFragment===setUserVisibleHint(false)
05-25 14:11:09.482 8685-8685/com.yunzhiyuan100.wish D/Linghu FindFragment.setUserVisibleHint: ===================FindFragment===setUserVisibleHint(false)
05-25 14:11:09.482 8685-8685/com.yunzhiyuan100.wish D/Linghu MineFragment.setUserVisibleHint: ===================MineFragment===setUserVisibleHint(false)
05-25 14:11:09.483 8685-8685/com.yunzhiyuan100.wish D/Linghu HomeFragment.setUserVisibleHint: ===================HomeFragment===setUserVisibleHint(true)
05-25 14:11:09.484 8685-8685/com.yunzhiyuan100.wish D/Linghu HomeFragment.onAttach: ===================HomeFragment===onAttach
05-25 14:11:09.484 8685-8685/com.yunzhiyuan100.wish D/Linghu HomeFragment.onCreate: ===================HomeFragment===onCreate
05-25 14:11:09.484 8685-8685/com.yunzhiyuan100.wish D/Linghu FindFragment.onAttach: ===================FindFragment===onAttach
05-25 14:11:09.485 8685-8685/com.yunzhiyuan100.wish D/Linghu FindFragment.onCreate: ===================FindFragment===onCreate
05-25 14:11:09.485 8685-8685/com.yunzhiyuan100.wish D/Linghu MineFragment.onAttach: ===================MineFragment===onAttach
05-25 14:11:09.486 8685-8685/com.yunzhiyuan100.wish D/Linghu MineFragment.onCreate: ===================MineFragment===onCreate
05-25 14:11:09.486 8685-8685/com.yunzhiyuan100.wish D/Linghu HomeFragment.onCreateView: ===================HomeFragment===onCreateView
05-25 14:11:09.609 8685-8685/com.yunzhiyuan100.wish D/Linghu HomeFragment.onViewCreated: ===================HomeFragment===onViewCreated
05-25 14:11:09.609 8685-8685/com.yunzhiyuan100.wish D/Linghu HomeFragment.onActivityCreated: ===================HomeFragment===onActivityCreated
05-25 14:11:09.615 8685-8685/com.yunzhiyuan100.wish D/Linghu HomeFragment.onStart: ===================HomeFragment===onStart
05-25 14:11:09.615 8685-8685/com.yunzhiyuan100.wish D/Linghu HomeFragment.onResume: ===================HomeFragment===onResume
05-25 14:11:09.616 8685-8685/com.yunzhiyuan100.wish D/Linghu FindFragment.onCreateView: ===================FindFragment===onCreateView
05-25 14:11:09.646 8685-8685/com.yunzhiyuan100.wish D/Linghu FindFragment.onViewCreated: ===================FindFragment===onViewCreated
05-25 14:11:09.646 8685-8685/com.yunzhiyuan100.wish D/Linghu FindFragment.onActivityCreated: ===================FindFragment===onActivityCreated
05-25 14:11:09.646 8685-8685/com.yunzhiyuan100.wish D/Linghu MineFragment.onCreateView: ===================MineFragment===onCreateView
05-25 14:11:09.695 8685-8685/com.yunzhiyuan100.wish D/Linghu MineFragment.onViewCreated: ===================MineFragment===onViewCreated
05-25 14:11:09.695 8685-8685/com.yunzhiyuan100.wish D/Linghu MineFragment.onActivityCreated: ===================MineFragment===onActivityCreated
05-25 14:11:09.695 8685-8685/com.yunzhiyuan100.wish D/Linghu FindFragment.onStart: ===================FindFragment===onStart
05-25 14:11:09.696 8685-8685/com.yunzhiyuan100.wish D/Linghu FindFragment.onResume: ===================FindFragment===onResume
05-25 14:11:09.696 8685-8685/com.yunzhiyuan100.wish D/Linghu MineFragment.onStart: ===================MineFragment===onStart
05-25 14:11:09.696 8685-8685/com.yunzhiyuan100.wish D/Linghu MineFragment.onResume: ===================MineFragment===onResume
点击切换到FindFragment(第二个tab)
05-25 14:13:46.807 8685-8685/com.yunzhiyuan100.wish D/Linghu HomeFragment.setUserVisibleHint: ===================HomeFragment===setUserVisibleHint(false)
05-25 14:13:46.808 8685-8685/com.yunzhiyuan100.wish D/Linghu FindFragment.setUserVisibleHint: ===================FindFragment===setUserVisibleHint(true)
点击切换到MineFragment(第三个tab)
05-25 14:13:48.659 8685-8685/com.yunzhiyuan100.wish D/Linghu FindFragment.setUserVisibleHint: ===================FindFragment===setUserVisibleHint(false)
05-25 14:13:48.660 8685-8685/com.yunzhiyuan100.wish D/Linghu MineFragment.setUserVisibleHint: ===================MineFragment===setUserVisibleHint(true)
3、总结:
Activity 生命周期走到可见,然后先执行Fragment的setUserVisibleHint(false)并把第一个Fragment置setUserVisibleHint(true), 然后依次执行Fragment的onAttach----onCreate, 然后依次onCreateView、onViewCreated、onActivityCreated,第一个Fragment的onstart、onResume一起执行了,然后最后是后两个Fragment的onStart、onResume.
Fragment的setUserVisibleHint()执行会在onAttach方法前面。
如果点击切换Fragment的时候会调用切换两个Fragment的setUserVisibleHint(false-----true)。
Fragment的OnResume方法和Activity的OnResume方法是调用时机是一致。