gpt4 book ai didi

android - onSaveInstanceState 不能保存静态变量?

转载 作者:行者123 更新时间:2023-11-30 04:52:49 32 4
gpt4 key购买 nike

考虑以下代码,它将静态变量:mMyArray 保存在 onSaveInstanceState 中,并在 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 中将值设置为 204-14 15:16:19.843: DEBUG/StaticVarActivity(7251): 在 onClick 中将值设置为 3

//改变配置,例如改变方向

04-14 15:16:23.893: DEBUG/StaticVarActivity(7251): onSaveInstanceState 1:3,304-14 15:16:23.893: DEBUG/StaticVarActivity(7251): onSaveInstanceState 2:3,304-14 15:16:24.073: DEBUG/StaticVarActivity(7251): 在 onCreate 中将值设置为 204-14 15:16:24.073: DEBUG/StaticVarActivity(7251): onRestoreInstanceState 1:2,204-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 中将值设置为 204-14 15:18:32.123: DEBUG/StaticVarActivity(7355): 在 onClick 中将值设置为 3

//改变配置,例如改变方向

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


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

最佳答案

据我所知,您不能将 static int[] 替换为另一个 int[]。您可以将 static int[] 的成员替换为其他 int 值。

关于android - onSaveInstanceState 不能保存静态变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2643562/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com