Android onRestoreInstanceState覆盖值

时间:2014-01-17 16:09:24

标签: android

所以我对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 = "";

感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

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";