简体   繁体   中英

Java - Android SDK - savedInstanceState & onSaveInstanceState

I'm trying to save the state of the counters (mCreate, mRestart...etc) and load them once the onCreate() method is established. For some reason it always revert back to '0' whenever onCreate() is called.

// Use these as keys when you're saving state between reconfigurations
private static final String RESTART_KEY = "restart";
private static final String RESUME_KEY = "resume";
private static final String START_KEY = "start";
private static final String CREATE_KEY = "create";

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Has previous state been saved?
    if (savedInstanceState != null) {           
        this.mCreate = savedInstanceState.getInt(CREATE_KEY);
        this.mRestart = savedInstanceState.getInt(RESTART_KEY);
        this.mStart = savedInstanceState.getInt(START_KEY);
        this.mResume = savedInstanceState.getInt(RESUME_KEY);
    }
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {    
savedInstanceState.containsKey(RESTART_KEY);
savedInstanceState.containsKey(RESUME_KEY);
savedInstanceState.containsKey(START_KEY);
savedInstanceState.containsKey(CREATE_KEY);
}

you should call

savedInstanceState.putInt(RESTART_KEY, _your_key_);

in the "onSaveInstanceState" methode

I checked this . I couldn't find any function "containsKEY" in the Bundle Class. However I think you should use

    savedInstanceState.putString("restartkey",RESTART_KEY);
    savedInstanceState.putInt("resumekey",RESUME_KEY);
    savedInstanceState.putBoolean("startkey",START_KEY);
    savedInstanceState.addString("createkey",CREATE_KEY);

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