活動被收回後的恢復 --12

當一個activity因內存不足被收回時,可以通過複寫onSaveInstanceState()方法將數據做臨時保存,並在onCreate()重新獲取,取出之後再做相應的恢復處理
代碼如下:

//for活動被收回後重新獲取被收回前頁面的資料
    //通過複寫onSaveInstanceState(Bundle)方法,並在onCreate()方法中重新獲取被回首前的值,並在進行恢復處理
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        // TODO Auto-generated method stub
        super.onSaveInstanceState(outState);
        String tempData = "Something you just typed";
        outState.putString("temp_data", tempData);
    }

Bundle方法通過putString()方法保存字符串,使用putInt()保存整型數據,以此類推。

//從 Bundle 中獲取暫存數據
        if(savedInstanceState != null){
            String tempData = savedInstanceState.getString("temp_data");
            Log.d("Demo_log", tempData);
        }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章