简体   繁体   中英

Android onRestoreInstanceState overwriting values

So I have a weird problem with onSaveInstanceState and onRestoreInstanceState. The following is what I had up to now, and which was working perfectly. Methods called normally, values saved and restored just fine.

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

Now I wanted to enhance the code a bit with a third value I wish to save.

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

The methods still get called perfectly but it's not restoring the correct values anymore. Instead of restoring the correct values into the correct variable, I get SAVE_STATE_LAST_STATE restored into all three vars ( mRawImgPath , mProImgPath and mLastState ).

What I can say is, they have the correct value when saving at onSaveInstanceState() so they do not have just the wrong value when saving. Also I do not change the values at onCreate() , otherwise the first solution (with only two variables) wouldn't work as well. I have really no idea what's happening there.

If it is of interest, here's the declaration:

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

Any help is appreciated.

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

you have empty names there, change to:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM