Fragment 內(nèi)部是一個狀態(tài)機行施,維護了六種狀態(tài)陪腌,如下:
未執(zhí)行onCreate或已執(zhí)行onDestroy:
static final int INITIALIZING = 0;
已執(zhí)行onCreate:
static final int CREATED = 1;
已執(zhí)行onActivityCreated:
static final int ACTIVITY_CREATED = 2;
已執(zhí)行onStop:
static final int STOPPED = 3;
已執(zhí)行onStart或者onPause
static final int STARTED = 4;
已執(zhí)行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绰咽,我的使命就是幫你提高