gpt4 book ai didi

Android onRestoreInstanceState 覆盖值

转载 作者:行者123 更新时间:2023-11-30 03:09:39 25 4
gpt4 key购买 nike

所以我对 onSaveInstanceState 和 onRestoreInstanceState 有一个奇怪的问题。以下是我到目前为止所拥有的,并且运行良好。方法正常调用,值保存和恢复都很好。

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
// Always call the superclass so it can restore the view hierarchy
super.onRestoreInstanceState(savedInstanceState);

// Restore value of members from saved state
mRawImgPath = savedInstanceState.getString(SAVE_STATE_RAW_IMG_PATH);
mProImgPath = savedInstanceState.getString(SAVE_STATE_PRO_IMG_PATH);
}

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
// Always call the superclass so it can save the view hierarchy state
super.onSaveInstanceState(savedInstanceState);

// Save the user's current game state
savedInstanceState.putString(SAVE_STATE_RAW_IMG_PATH, mRawImgPath);
savedInstanceState.putString(SAVE_STATE_PRO_IMG_PATH, mProImgPath);
}

现在我想用我希望保存的第三个值来增强代码。

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
// Always call the superclass so it can restore the view hierarchy
super.onRestoreInstanceState(savedInstanceState);

// Restore value of members from saved state
mRawImgPath = savedInstanceState.getString(SAVE_STATE_RAW_IMG_PATH);
mProImgPath = savedInstanceState.getString(SAVE_STATE_PRO_IMG_PATH);
mLastState = savedInstanceState.getString(SAVE_STATE_LAST_STATE);
}

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
// Always call the superclass so it can save the view hierarchy state
super.onSaveInstanceState(savedInstanceState);

// Save the user's current game state
savedInstanceState.putString(SAVE_STATE_RAW_IMG_PATH, mRawImgPath);
savedInstanceState.putString(SAVE_STATE_PRO_IMG_PATH, mProImgPath);
savedInstanceState.putString(SAVE_STATE_LAST_STATE, mLastState);
}

这些方法仍然可以完美调用,但它不再恢复正确的值。我没有将正确的值恢复到正确的变量中,而是将 SAVE_STATE_LAST_STATE 恢复到所有三个变量中(mRawImgPathmProImgPathmLastState)。

我能说的是,它们在 onSaveInstanceState() 中保存时具有正确的值,因此它们在保存时不会有错误的值。此外,我不会更改 onCreate() 中的值,否则第一个解决方案(只有两个变量)将无法正常工作。我真的不知道那里发生了什么。

如果有兴趣,这里是声明:

// Vars
GPSTracker gps;
String mRawImgPath = ""; // Path of the raw image
String mProImgPath = ""; // Path of the processed image
String mLastState = "";

// Save state variables
static final String SAVE_STATE_RAW_IMG_PATH = "";
static final String SAVE_STATE_PRO_IMG_PATH = "";
static final String SAVE_STATE_LAST_STATE = "";

感谢任何帮助。

最佳答案

static final String SAVE_STATE_RAW_IMG_PATH = "";
static final String SAVE_STATE_PRO_IMG_PATH = "";
static final String SAVE_STATE_LAST_STATE = "";

你的名字是空的,改成:

static final String SAVE_STATE_RAW_IMG_PATH = "SAVE_STATE_RAW_IMG_PATH";
static final String SAVE_STATE_PRO_IMG_PATH = "SAVE_STATE_PRO_IMG_PATH";
static final String SAVE_STATE_LAST_STATE = "SAVE_STATE_LAST_STATE";

关于Android onRestoreInstanceState 覆盖值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21190514/

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