http://www.reibang.com/p/89e0a7533dbe
寫的很好~~ 作為參考
- 起因
發(fā)現(xiàn)在手機里菜單鍵, 然后點擊叉叉, 殺了進程后, 再打開, 發(fā)現(xiàn):
ScrollView 會自動滾回剛才離開時的位置
TextView 會自動設置成剛才離開時的text
后來看ScrollView的實現(xiàn), 發(fā)現(xiàn)在每次殺進程后再打開進程, 會執(zhí)行onRestoreInstanceState并傳入?yún)?shù), 參數(shù)中有一項 ScrollPosition記錄的是剛才滾動的位置. 當此ScrollView在重新打開后第一次layout的時候, 會取到這個scrollPosition, 并scrollto相應的位置
TextView實現(xiàn)也是類似, 重寫了onSaveInstanceState 記錄狀態(tài), 并onRestoreInstanceState恢復狀態(tài).
onSaveInstanceState在應用有可能在用戶非有意情況下銷毀的時候執(zhí)行. onPause 和onStop 執(zhí)行, 順序不一定
onRestoreInstanceState 在應用確實銷毀了后, 再重新打開時執(zhí)行, onCreate之后, onStart之后.順便看了Parcelable(store數(shù)據(jù)的基本格式), 大概是本來一個java的對象, 變成Parcelable的格式可以進行存儲和傳輸. 等下次你拿到還可以恢復.
重要:
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()用的是同樣的恢復數(shù)據(jù)........
- Activity 和 view的 onSaveInstanceState and onRestoreInstanceState
在Activity的保存的狀態(tài)里有一項: Key=android:viewHierarchyState, content=null 保存的是view的狀態(tài). Activity 的save和restore的時候, 要通過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}