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,我的使命就是帮你提高
网友评论