开发者

Static variables can't be saved in onSaveInstanceState?

Consider following code, it saves static variable: mMyArray in onSaveInstanceState and restore it in onRestoreInstanceState, however I found it can't save the int array at all. I tried primative type value (int), it can save/restore primative type value.

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]);
    }
}

The log:


04-14 15:16:00.633: DEBUG/StaticVarActivity(7251): Setting values to 2 in onCreate 04-14 15:16:19.843: DEBUG/StaticVarActivity(7251): Setting values to 3 in onClick

// Make configure change, e.g change orientation

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): Setting values to 2 in onCreate 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


If I change to:

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

The log will be:


04-14 15:18:29.183: DEBUG/StaticVarActivity(7355): Setting values to 2 in onCreate 04-14 15:18:32.123: DEBUG/StaticVarActivity(7355): Setting values to 3 in onClick

// Make configure change, e.g change orientation

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): Setting values to 2 in onCreate 04-14 15:18:33.393: DEBUG/StaticVarActivity(7355): onRestoreInstanceState 1:2,2 04-14 15:18:开发者_Python百科33.393: DEBUG/StaticVarActivity(7355): onRestoreInstanceState 2:3,3


I got to know it doesn't need to save the static variable, but why this happen?


AFAIK, you cannot replace a static int[] with another int[]. You can replace members of a static int[] with other int values.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜