静态变量无法保存在onSaveInstanceState中?

时间:2010-04-15 07:55:40

标签: android

考虑下面的代码,它在onSaveInstanceState中保存静态变量:mMyArray并在onRestoreInstanceState中恢复它,但是我发现它根本无法保存int数组。我尝试了灵长类型值(int),它可以保存/恢复原始类型值。

public class StaticVarActivity extends Activity {

    private static final String TAG = "StaticVarActivity";

    private static int[] mMyArray = { 1, 1 };

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Log.d(TAG, "Setting values to 2 in onCreate");
        mMyArray[0] = 2;
        mMyArray[1] = 2;

        findViewById(R.id.btn).setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                Log.d(TAG, "Setting values to 3 in onClick");
                mMyArray[0] = 3;
                mMyArray[1] = 3;
            }
        });
    }

    /* (non-Javadoc)
     * @see android.app.Activity#onRestoreInstanceState(android.os.Bundle)
     */
    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);

        Log.d(TAG,"onRestoreInstanceState 1:"+mMyArray[0]+","+mMyArray[1]);
        mMyArray = savedInstanceState.getIntArray("key");
        Log.d(TAG,"onRestoreInstanceState 2:"+mMyArray[0]+","+mMyArray[1]);

    }

    /* (non-Javadoc)
     * @see android.app.Activity#onSaveInstanceState(android.os.Bundle)
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        Log.d(TAG,"onSaveInstanceState 1:"+mMyArray[0]+","+mMyArray[1]);
        //int[] sss = new int[]{mMyArray[0],mMyArray[1]};
        //outState.putIntArray("key", sss);
        outState.putIntArray("key", mMyArray);
        Log.d(TAG,"onSaveInstanceState 2:"+mMyArray[0]+","+mMyArray[1]);
    }
}

日志:


04-14 15:16:00.633:DEBUG / StaticVarActivity(7251):在onCreate中将值设置为2 04-14 15:16:19.843:DEBUG / StaticVarActivity(7251):在onClick中将值设置为3

//进行配置更改,例如更改方向

04-14 15:16:23.893:DEBUG / StaticVarActivity(7251):onSaveInstanceState 1:3,3 04-14 15:16:23.893:DEBUG / StaticVarActivity(7251):onSaveInstanceState 2:3,3 04-14 15:16:24.073:DEBUG / StaticVarActivity(7251):在onCreate中将值设置为2 04-14 15:16:24.073:DEBUG / StaticVarActivity(7251):onRestoreInstanceState 1:2,2 04-14 15:16:24.073:DEBUG / StaticVarActivity(7251):onRestoreInstanceState 2:2,2


如果我改为:

private int[] mMyArray = { 1, 1 };

日志将是:


04-14 15:18:29.183:DEBUG / StaticVarActivity(7355):在onCreate中将值设置为2 04-14 15:18:32.123:DEBUG / StaticVarActivity(7355):在onClick中将值设置为3

//进行配置更改,例如更改方向

04-14 15:18:33.353:DEBUG / StaticVarActivity(7355):onSaveInstanceState 1:3,3 04-14 15:18:33.353:DEBUG / StaticVarActivity(7355):onSaveInstanceState 2:3,3 04-14 15:18:33.373:DEBUG / StaticVarActivity(7355):在onCreate中将值设置为2 04-14 15:18:33.393:DEBUG / StaticVarActivity(7355):onRestoreInstanceState 1:2,2 04-14 15:18:33.393:DEBUG / StaticVarActivity(7355):onRestoreInstanceState 2:3,3


我知道它不需要保存静态变量,但为什么会这样呢?

1 个答案:

答案 0 :(得分:1)

AFAIK,您无法将static int[]替换为另一个int[]。您可以将static int[]的成员替换为其他int值。