Android笔记:onSaveInstanceState和onRestoreInstanceState总结
一、方法調用時間
onSaveInstanceState是用來保存UI狀態,在Activity殺死之前,它一般在onStop或者onPause之前觸發;
onRestoreInstanceState是在onResume之前觸發來恢復狀態;
Activity被殺死了,onCreate會被調用,且onRestoreInstanceState在 onResume之前恢復上次保存的信息;
Activity沒被殺死,onCreate不會被調用,但onRestoreInstanceState 仍然會被調用,在 onResume之前恢復上次保存的信息;
二、方法使用場景?
onSaveInstanceState() 只有在Acitivity被系統kill掉時才會調用。所以通常onSaveInstanceState()只適合用于保存一些臨時性的狀態,而onPause()適合用于數據的持久化保存。
onSaveInstanceState()方法只適合保存瞬態數據, 比如UI控件的狀態, 成員變量的值等,而不應該用來保存持久化數據,持久化數據應該當用戶離開當前的activity時,在onPause()中保存(比如將數據保存到數據庫或文件中)。說到這里,還要說一點的就是在onPause()中不適合用來保存比較費時的數據,所以這點要理解。
由于onSaveInstanceState()方法方法不一定會被調用, 因此不適合在該方法中保存持久化數據, 例如向數據庫中插入記錄等. 保存持久化數據的操作應該放在onPause()中。若是永久性值,則在onPause()中保存;若大量,則另開線程吧,別阻塞UI線程。?
三、使用方法
1.在onSaveInstanceState方法中保存bundle:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | ????@Override ????protected?void?onSaveInstanceState(Bundle?outState) ????{ ????????super.onSaveInstanceState(outState); ????????mMapView.onSaveInstanceState(outState); ????????? ????????saveState(outState); ????} ????? ????@Override ????protected?void?onRestoreInstanceState(Bundle?InState) ????{ ????????super.onRestoreInstanceState(InState); ????????mMapView.onSaveInstanceState(InState); ????????? ????????restoreState(InState); ????} |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | ????/** ?????*?進入三維模塊后,該activity會被kill掉,此處保存被殺掉前的一些狀態數據 ?????*? ?????*?@param?outState ?????*/ ????private?void?saveState(Bundle?outState) ????{ ????????outState.putParcelable(HBContant.KEY_STATE_ESTATEINFO_JSON,?mJson); ????????outState.putParcelable(HBContant.KEY_STATE_ESTATEINFO_GALLERY,?mGalleryJson); ????????outState.putParcelable(HBContant.KEY_STATE_ESTATEINFO_FLASH,?mFlashJson); ????????outState.putParcelableArrayList(HBContant.KEY_STATE_ESTATEINFO_VIDEO,?mVideoList); ????????outState.putParcelableArrayList(HBContant.KEY_STATE_ESTATEINFO_NEWS,?mNewsList); ????????outState.putParcelableArrayList(HBContant.KEY_STATE_ESTATEINFO_HOUSELIST,?mEstateHouseList); ????} ????? ????private?void?restoreState(Bundle?inState) ????{ ????} |
2.在oncreate或者onRestoreInstanceState方法中讀取保存的bundle:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | ????????if?(savedInstanceState?==?null) ????????{ ????????????//正常情況 ????????????loadTask(); ????????} ????????else ????????{ ????????????//進入三維圖被kill后返回,恢復頁面數據 ????????????mJson?=?savedInstanceState.getParcelable(HBContant.KEY_STATE_ESTATEINFO_JSON); ????????????mGalleryJson?=?savedInstanceState.getParcelable(HBContant.KEY_STATE_ESTATEINFO_GALLERY); ????????????mFlashJson?=?savedInstanceState.getParcelable(HBContant.KEY_STATE_ESTATEINFO_FLASH); ????????????mVideoList?=?savedInstanceState.getParcelableArrayList(HBContant.KEY_STATE_ESTATEINFO_VIDEO); ????????????mNewsList?=?savedInstanceState.getParcelableArrayList(HBContant.KEY_STATE_ESTATEINFO_NEWS); ????????????mEstateHouseList?=?savedInstanceState.getParcelableArrayList(HBContant.KEY_STATE_ESTATEINFO_HOUSELIST); ????????????? ????????????? ????????????if(mJson?!=?null?&&?mGalleryJson?!=?null?&&?mFlashJson?!=?null?&&?mVideoList?!=?null?&&?mEstateHouseList?!=?null) ????????????{ ????????????????loadComplete(); ????????????} ????????????else ????????????{ ????????????????loadTask(); ????????????} ????????} |
本文轉自 glblong 51CTO博客,原文鏈接:http://blog.51cto.com/glblong/1432616,如需轉載請自行聯系原作者
總結
以上是生活随笔為你收集整理的Android笔记:onSaveInstanceState和onRestoreInstanceState总结的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [开发笔记]-DataGridView控
- 下一篇: [Android] DownloadMa