2014-01-17 117 views
1

所以我有一個奇怪的問題onSaveInstanceState和onRestoreInstanceState。 以下是我到目前爲止所做的工作,並且完美地工作。通常稱爲方法,值保存並恢復正常。Android 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恢復到所有三個變量(mRawImgPath,mProImgPathmLastState)。

我可以說的是,他們在保存在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
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"; 
+0

*蕩*美麗。我不記得我必須聲明變量的名稱爲值。非常感謝。 – user3207296

+0

不客氣:) – isah