Android笔记:onSaveInstanceState和onRestoreInstanceState总结
一、方法調(diào)用時間
onSaveInstanceState是用來保存UI狀態(tài),在Activity殺死之前,它一般在onStop或者onPause之前觸發(fā);
onRestoreInstanceState是在onResume之前觸發(fā)來恢復(fù)狀態(tài);
Activity被殺死了,onCreate會被調(diào)用,且onRestoreInstanceState在 onResume之前恢復(fù)上次保存的信息;
Activity沒被殺死,onCreate不會被調(diào)用,但onRestoreInstanceState 仍然會被調(diào)用,在 onResume之前恢復(fù)上次保存的信息;
二、方法使用場景?
onSaveInstanceState() 只有在Acitivity被系統(tǒng)kill掉時才會調(diào)用。所以通常onSaveInstanceState()只適合用于保存一些臨時性的狀態(tài),而onPause()適合用于數(shù)據(jù)的持久化保存。
onSaveInstanceState()方法只適合保存瞬態(tài)數(shù)據(jù), 比如UI控件的狀態(tài), 成員變量的值等,而不應(yīng)該用來保存持久化數(shù)據(jù),持久化數(shù)據(jù)應(yīng)該當(dāng)用戶離開當(dāng)前的activity時,在onPause()中保存(比如將數(shù)據(jù)保存到數(shù)據(jù)庫或文件中)。說到這里,還要說一點的就是在onPause()中不適合用來保存比較費時的數(shù)據(jù),所以這點要理解。
由于onSaveInstanceState()方法方法不一定會被調(diào)用, 因此不適合在該方法中保存持久化數(shù)據(jù), 例如向數(shù)據(jù)庫中插入記錄等. 保存持久化數(shù)據(jù)的操作應(yīng)該放在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掉,此處保存被殺掉前的一些狀態(tài)數(shù)據(jù) ?????*? ?????*?@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后返回,恢復(fù)頁面數(shù)據(jù) ????????????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(); ????????????} ????????} |
本文轉(zhuǎn)自 glblong 51CTO博客,原文鏈接:http://blog.51cto.com/glblong/1432616,如需轉(zhuǎn)載請自行聯(lián)系原作者
總結(jié)
以上是生活随笔為你收集整理的Android笔记:onSaveInstanceState和onRestoreInstanceState总结的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [开发笔记]-DataGridView控
- 下一篇: Internet Explorer 9.