Activity生命周期分析

Fragment 內(nèi)部是一個(gè)狀態(tài)機(jī),維護(hù)了六種狀態(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及對(duì)應(yīng)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進(jìn)階群 群號(hào):271165123,我的使命就是幫你提高

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1 概述 在上述的多種狀態(tài)中,只有三個(gè)狀態(tài)是持久的,其余都是瞬時(shí)狀態(tài):resumed,paused,stopped...
    lee_3do閱讀 984評(píng)論 0 1
  • Activity與Fragment的生命周期是一個(gè)老生常談的話題,網(wǎng)上最常見的圖是這個(gè): 還有這個(gè): 場(chǎng)景一 我的...
    PaperFish閱讀 781評(píng)論 0 1
  • onCreate():表示Activity正在創(chuàng)建,可以做初始化操作,setContentView(),初始化Ac...
    西鳳梨閱讀 416評(píng)論 0 0
  • 從activity的創(chuàng)建到銷毀,會(huì)走onCreate()、onStart()、onResume()、onPause...
    劉孫貓咪閱讀 15,501評(píng)論 2 4
  • Activity生命周期分析 標(biāo)準(zhǔn)情況下 onCreate():表示Activity正在創(chuàng)建中,初始化工作,比如調(diào)...
    myserendipit閱讀 527評(píng)論 1 7

友情鏈接更多精彩內(nèi)容