onSaveInstanceState and onRestoreInstanceState

http://m.itdecent.cn/p/89e0a7533dbe

寫的很好~~ 作為參考

  1. 起因
    發(fā)現(xiàn)在手機(jī)里菜單鍵, 然后點(diǎn)擊叉叉, 殺了進(jìn)程后, 再打開, 發(fā)現(xiàn):
    ScrollView 會(huì)自動(dòng)滾回剛才離開時(shí)的位置
    TextView 會(huì)自動(dòng)設(shè)置成剛才離開時(shí)的text

后來看ScrollView的實(shí)現(xiàn), 發(fā)現(xiàn)在每次殺進(jìn)程后再打開進(jìn)程, 會(huì)執(zhí)行onRestoreInstanceState并傳入?yún)?shù), 參數(shù)中有一項(xiàng) ScrollPosition記錄的是剛才滾動(dòng)的位置. 當(dāng)此ScrollView在重新打開后第一次layout的時(shí)候, 會(huì)取到這個(gè)scrollPosition, 并scrollto相應(yīng)的位置

TextView實(shí)現(xiàn)也是類似, 重寫了onSaveInstanceState 記錄狀態(tài), 并onRestoreInstanceState恢復(fù)狀態(tài).

  1. onSaveInstanceState在應(yīng)用有可能在用戶非有意情況下銷毀的時(shí)候執(zhí)行. onPause 和onStop 執(zhí)行, 順序不一定
    onRestoreInstanceState 在應(yīng)用確實(shí)銷毀了后, 再重新打開時(shí)執(zhí)行, onCreate之后, onStart之后.

  2. 順便看了Parcelable(store數(shù)據(jù)的基本格式), 大概是本來一個(gè)java的對(duì)象, 變成Parcelable的格式可以進(jìn)行存儲(chǔ)和傳輸. 等下次你拿到還可以恢復(fù).

  3. 重要:
    When your activity is recreated after it was previously destroyed, you can recover your saved state from the Bundle that the system passes your activity. Both the onCreate() and onRestoreInstanceState() callback methods receive the same Bundle that contains the instance state information.

Because the onCreate() method is called whether the system is creating a new instance of your activity or recreating a previous one, you must check whether the state Bundle is null before you attempt to read it. If it is null, then the system is creating a new instance of the activity, instead of restoring a previous one that was destroyed.
onCreate() 和 onRestoreInstanceState()用的是同樣的恢復(fù)數(shù)據(jù)........

  1. Activity 和 view的 onSaveInstanceState and onRestoreInstanceState
    在Activity的保存的狀態(tài)里有一項(xiàng): Key=android:viewHierarchyState, content=null 保存的是view的狀態(tài). Activity 的save和restore的時(shí)候, 要通過mWindow調(diào)用 mContentParent的所有子view的save和restore.
    自己打印的格式, 大概可以看:
    Key=android:viewHierarchyState, content = bundle: {
    Key=android:views, content = array1: { null
    Key=android:views, content = array2: { {16908290=android.view.AbsSavedState$1@b1b4601, 2131623936=FragmentPager.SavedState{fb74ca6 position=0}, 2131623937=android.view.AbsSavedState$1@b1b4601, 2131623938=android.view.AbsSavedState$1@b1b4601, 2131623939=android.view.AbsSavedState$1@b1b4601, 2131623940=android.view.AbsSavedState$1@b1b4601, 2131623990=android.view.AbsSavedState$1@b1b4601, 2131623991=android.view.AbsSavedState$1@b1b4601, 2131623992=android.view.AbsSavedState$1@b1b4601, 2131623993=android.view.AbsSavedState$1@b1b4601, 2131624000=miui.widget.ScreenView$SavedState@8ca66e7, 2131624001=android.view.AbsSavedState$1@b1b4601, 2131624002=android.view.AbsSavedState$1@b1b4601, 2131624003=TextView.SavedState{7326294 start=6 end=6 text=七十二層奇樓}, 2131624004=android.view.AbsSavedState$1@b1b4601, 2131624005=android.view.AbsSavedState$1@b1b4601, 2131624006=HorizontalScrollView.SavedState{cf5413d scrollPosition=0}, 2131624020=android.view.AbsSavedState$1@b1b4601, 2131624026=android.view.AbsSavedState$1@b1b4601, 2131624030=android.view.AbsSavedState$1@b1b4601, 2131624031=android.view.AbsSavedState$1@b1b4601, 2131624032=android.view.AbsSavedState$1@b1b4601, 2131624033=android.view.AbsSavedState$1@b1b4601, 2131624034=android.view.AbsSavedState$1@b1b4601}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,366評(píng)論 25 708
  • 初識(shí)Activity: 我們都知道android中有四大組件(Activity活動(dòng),Service服務(wù),Conte...
    wangling90閱讀 1,119評(píng)論 2 2
  • 葉酸 很久沒有逛街買東西了。 自從買了房子已來,基本和逛街購物絕緣了。 天天扣著錢過日子,真清苦。 老公也不爭氣,...
    子持子持閱讀 227評(píng)論 0 0
  • 昨天寫了《如何挑選伴侶?你挑對(duì)了嗎?》也是有感而發(fā),因?yàn)槲抑鴮?shí)覺得我老公挑對(duì)了人。 我和老公相識(shí)于我大學(xué)畢業(yè)那年,...
    樂簡家閱讀 651評(píng)論 0 0
  • 簡介 MD5(單向散列算法)的全稱是Message-Digest Algorithm 5(信息-摘要算法),經(jīng)過M...
    帥只是表象閱讀 3,739評(píng)論 3 14

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