简体   繁体   中英

android TextView onRestoreInstanceState get wrong value

My code is like this:

    @Override
    public Parcelable onSaveInstanceState() {
        Parcelable superState = super.onSaveInstanceState();
        SavedState savedState = new SavedState(superState);
        savedState.progress = mProgress;
        return savedState;
    }

    @Override
    public void onRestoreInstanceState(Parcelable state) {
            SavedState savedState = (SavedState) state;
            mProgress = savedState.progress;
            super.onRestoreInstanceState(savedState.getSuperState());
    }

The mProgress value is 60 when I pass it to savedState in onSaveInstanceState and I get 0 in onRestoreInstanceState .

if I modify the code savedState.mProgress = mProgress in onSavedInstanceState to savedState.mProgress = 10 then I got 10 in onRestoreInstanceState .

It's so weird. if I make the mProgress static then I get 60. I don't know what's the matter.

Use like this,

@Override
protected void onCreate(Bundle savedInstanceState) {
    if (savedInstanceState != null) {
        mProgress = savedInstanceState.getInt("progress");            
    }
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt("progress", mProgress);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    if (savedInstanceState != null) {
        mProgress = savedInstanceState.getInt("progress");            
    }
}

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