Shark1103
Shark1103

Reputation: 1

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

Upvotes: 0

Views: 62

Answers (2)

Yash Jain
Yash Jain

Reputation: 374

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

Upvotes: 0

Sofiane Daoud
Sofiane Daoud

Reputation: 878

you should call

savedInstanceState.putInt(RESTART_KEY, _your_key_);

in the "onSaveInstanceState" methode

Upvotes: 1

Related Questions