简体   繁体   English

Java-Android SDK-savedInstanceState和onSaveInstanceState

[英]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. 我正在尝试保存计数器的状态(mCreate,mRestart ...等),并在onCreate()方法建立后加载它们。 For some reason it always revert back to '0' whenever onCreate() is called. 由于某种原因,每次调用onCreate()时,它总是恢复为“ 0”。

// 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 在“ onSaveInstanceState”方法中

I checked this . 我检查了这个 I couldn't find any function "containsKEY" in the Bundle Class. 我在Bundle类中找不到任何函数“ containsKEY”。 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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