Android保存當前活動狀態

在學院科技文化節的時候作了一個安卓小應用,實現了:加載圖片——人臉分析——添加搞怪表情。
問題:
加載了圖片或者添加了表情以後,鎖屏,解鎖,界面回到了原始狀態(圖片丟失了)。
緣由:
當解鎖的時候,自動調用了一次oncreat,變量們都從新new了一次,因此就回到原始狀態了。
解決:
當解鎖屏幕的時候,方法調用以下圖
請輸入圖片描述
咱們須要作的是,重寫 onSaveInstanceState 方法。android

To save additional state information for your activity, you must implement onSaveInstanceState() and add key-value pairs to the Bundle object——android 官網segmentfault

代碼以下:ide

@Override
    protected void onSaveInstanceState(Bundle outState) {        //保存狀態,防止鎖屏後圖片丟失
        // TODO Auto-generated method stub
        outState.putParcelable("NewBitmap",NewBitmap);
        outState.putParcelable("img", img);
        outState.putParcelable("bitmap", bitmap);
        outState.putInt("ifLoaded", ifLoaded);
        outState.putFloat("eye_left_x", eye_left_x);
        outState.putFloat("eye_left_y", eye_left_y);
        outState.putFloat("eye_right_x", eye_right_x);
        outState.putFloat("eye_right_y", eye_right_y);
        outState.putFloat("nose_x", nose_x);
        outState.putFloat("nose_y", nose_y);
        outState.putFloat("mouth_x", mouth_x);  
        outState.putFloat("mouth_y", mouth_y);
        System.out.println("onSaveInstanceState");
        super.onSaveInstanceState(outState);
    }

把須要保存的數據都put進這個Bundle對象。spa

我這裏保存的是,bitmap,和一些用來畫表情的一些座標(眼耳口鼻)。code

重寫onRestoreInstanceState把剛纔保存的值取出來:orm

@Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {    //恢復狀態數據
        // TODO Auto-generated method stub
        ifLoaded=savedInstanceState.getInt("ifLoaded");
        NewBitmap=savedInstanceState.getParcelable("NewBitmap");
        img=savedInstanceState.getParcelable("img");
        bitmap=savedInstanceState.getParcelable("bitmap");
        eye_left_x=savedInstanceState.getFloat("eye_left_x");
        eye_left_y=savedInstanceState.getFloat("eye_left_y");
        eye_right_x=savedInstanceState.getFloat("eye_right_x");
        eye_right_y=savedInstanceState.getFloat("eye_right_y");
        nose_x=savedInstanceState.getFloat("nose_x");
        nose_y=savedInstanceState.getFloat("nose_y");
        mouth_x=savedInstanceState.getFloat("mouth_x");
        mouth_y=savedInstanceState.getFloat("mouth_y");
        if(img!=null){
            imageView.setImageBitmap(img);
        }

        System.out.println("onRestoreInstanceState");
        super.onRestoreInstanceState(savedInstanceState);
    }

OK,問題解決了。對象

以上是方法一,還有一種方法就是這樣的:圖片

引用官網的代碼,get

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); // Always call the superclass first

    // Check whether we're recreating a previously destroyed instance
    if (savedInstanceState != null) {
        // Restore value of members from saved state
        mCurrentScore = savedInstanceState.getInt(STATE_SCORE);
        mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL);
    } else {
        // Probably initialize members with default values for a new instance
    }
    ...
}

換成個人代碼的話,我只需變更 if 裏面的代碼:it

if(savedInstanceState!=null){
            ifLoaded=savedInstanceState.getInt("ifLoaded");
            NewBitmap=savedInstanceState.getParcelable("NewBitmap");
            img=savedInstanceState.getParcelable("img");
            bitmap=savedInstanceState.getParcelable("bitmap");
            eye_left_x=savedInstanceState.getFloat("eye_left_x");
            eye_left_y=savedInstanceState.getFloat("eye_left_y");
            eye_right_x=savedInstanceState.getFloat("eye_right_x");
            eye_right_y=savedInstanceState.getFloat("eye_right_y");
            nose_x=savedInstanceState.getFloat("nose_x");
            nose_y=savedInstanceState.getFloat("nose_y");
            mouth_x=savedInstanceState.getFloat("mouth_x");
            mouth_y=savedInstanceState.getFloat("mouth_y");
            if(img!=null){
                imageView.setImageBitmap(img);
            }

        }

也是這樣就能夠把保存的數據取出來了。

個人馥這幾天不知道怎麼了。~又耍脾氣不理我了~女人心真難懂~唉。

相關文章
相關標籤/搜索