http://m.itdecent.cn/p/89e0a7533dbe
寫的很好~~ 作為參考
- 起因
發(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).
onSaveInstanceState在應(yīng)用有可能在用戶非有意情況下銷毀的時(shí)候執(zhí)行. onPause 和onStop 執(zhí)行, 順序不一定
onRestoreInstanceState 在應(yīng)用確實(shí)銷毀了后, 再重新打開時(shí)執(zhí)行, onCreate之后, onStart之后.順便看了Parcelable(store數(shù)據(jù)的基本格式), 大概是本來一個(gè)java的對(duì)象, 變成Parcelable的格式可以進(jìn)行存儲(chǔ)和傳輸. 等下次你拿到還可以恢復(fù).
重要:
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ù)........
- 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}