Activity生命周期分析

Fragment 内部是一个状态机,维护了六种状态,如下:

未执行onCreate或已执行onDestroy:
static final int INITIALIZING = 0;

已执行onCreate:
static final int CREATED = 1;

已执行onActivityCreated:
static final int ACTIVITY_CREATED = 2;

已执行onStop:
static final int STOPPED = 3;

已执行onStart或者onPause
static final int STARTED = 4;

已执行onResume
static final int RESUMED = 5;

Activity及对应Fragment的生命周期切换流程:

Activity—>>performCreate(Bundle icicle)
               onCreate(icicle)
                   mFragments.dispatchCreate()
                       mCurState=Fragment.CREATED
                       moveToState()//move all active fragments to mCurState
               mFragments.dispatchActivityCreated()
                   mCurState=Fragment.ACTIVITY_CREATED
                   moveToState()//move all active fragments to mCurState

Activity—>>performStart()
               callActivityOnStart(this)
               mFragments.dispatchStart()
                   mCurState=Fragment.STARTED
                   moveToState()//move all active fragments to mCurState

Activity—>>performRestoreInstanceState(Bundle savedInstanceState)
               onRestoreInstanceState(savedInstanceState)

Activity—>>onPostCreate(@Nullable Bundle savedInstanceState)

Activity—>>performResume()
               callActivityOnResume(this)
               mFragments.dispatchResume()
                   mCurState=Fragment.RESUMED
                   moveToState()//move all active fragments to mCurState
               onPostResume()

Activity—>>performSaveInstanceState(Bundle outState)
               onSaveInstanceState(outState)

Activity—>>performPause()
               mFragments.dispatchPause()
                   mCurState=Fragment.STARTED
                   moveToState()//move all active fragments to mCurState
               onPause()

Activity—>>performStop()
               mFragments.dispatchStop()
                   mCurState=Fragment.STOPPED
                   moveToState()//move all active fragments to mCurState
               callActivityOnStop(this)

Activity—>>performDestroy()
               mFragments.dispatchDestroy()
                   mCurState=Fragment.INITIALIZING
                   moveToState()//move all active fragments to mCurState
               onDestroy();

欢迎加入android进阶群 群号:271165123,我的使命就是帮你提高

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容