一个FragmentActivity多个Fragment的生命周期事件记录-LMLPHP

  • 初次打开FragmentActivity时

     VisitTaskManagerActivity(): onCreate
    VisitTaskManagerActivity(): onStart
    VisitTaskManagerActivity(): onResume Fragment1(): onAttach
    Fragment1(): onCreate
    Fragment1(): onCreateView
    Fragment1(): onViewCreated
    Fragment1(): onActivityCreated
    Fragment1(): onViewStateRestored
    Fragment1(): onStart
    Fragment1(): onResume Fragment2(): onAttach
    Fragment2(): onCreate
    Fragment2(): onCreateView
    Fragment2(): onViewCreated
    Fragment2(): onActivityCreated
    Fragment2(): onViewStateRestored Fragment3(): onAttach
    Fragment3(): onCreate
    Fragment3(): onCreateView
    Fragment3(): onViewCreated
    Fragment3(): onActivityCreated
    Fragment3(): onViewStateRestored Fragment4(): onAttach
    Fragment4(): onCreate
    Fragment4(): onCreateView
    Fragment4(): onViewCreated
    Fragment4(): onActivityCreated
    Fragment4(): onViewStateRestored Fragment2(): onStart
    Fragment2(): onResume Fragment3(): onStart
    Fragment3(): onResume Fragment4(): onStart
    Fragment4(): onResume
  • 系统按HOME键到桌面时
    VisitTaskManagerActivity(): onPause
    Fragment1(): onPause
    Fragment2(): onPause
    Fragment3(): onPause
    Fragment4(): onPause
    VisitTaskManagerActivity(): onStop
    Fragment1(): onStop
    Fragment2(): onStop
    Fragment3(): onStop
    Fragment4(): onStop
  • 从桌面返回FragmentActivity时
     VisitTaskManagerActivity(): onStart
    Fragment1(): onStart
    Fragment2(): onStart
    Fragment3(): onStart
    Fragment4(): onStart
    VisitTaskManagerActivity(): onResume
    Fragment1(): onResume
    Fragment2(): onResume
    Fragment3(): onResume
    Fragment4(): onResume
  • 自动切换横竖屏时(或者按HOME键到桌面时,杀掉进程,再从桌面返回FragmentActivity时)
    VisitTaskManagerActivity(): onCreate
    
    Fragment1(): onAttach
    Fragment1(): onCreate Fragment2(): onAttach
    Fragment2(): onCreate Fragment3(): onAttach
    Fragment3(): onCreate Fragment4(): onAttach
    Fragment4(): onCreate VisitTaskManagerActivity(): onStart Fragment1(): onCreateView
    Fragment1(): onViewCreated
    Fragment1(): onActivityCreated
    Fragment1(): onViewStateRestored Fragment2(): onCreateView
    Fragment2(): onViewCreated
    Fragment2(): onActivityCreated
    Fragment2(): onViewStateRestored Fragment3(): onCreateView
    Fragment3(): onViewCreated
    Fragment3(): onActivityCreated
    Fragment3(): onViewStateRestored Fragment4(): onCreateView
    Fragment4(): onViewCreated
    Fragment4(): onActivityCreated
    Fragment4(): onViewStateRestored Fragment1(): onStart
    Fragment2(): onStart
    Fragment3(): onStart
    Fragment4(): onStart VisitTaskManagerActivity(): onResume
    Fragment1(): onResume
    Fragment2(): onResume
    Fragment3(): onResume
    Fragment4(): onResume

在此使用onSaveInstanceState来保存临时状态的需留意的:

  1. 根据官方文档的说明,onSaveInstanceState的调用时机为非正常finish()情况下,可能在onPause到onDestroy之间的任意时刻调用。
    onSaveInstanceState | Android Developers
05-07 15:50