PrototypeZ / SaveState

:icecream: Generate 'onSaveInstanceState' and 'onRestoreInstanceState' methods for your application automatically
1.01k stars 79 forks source link

When is the data stored ? #8

Closed abiemann closed 5 years ago

abiemann commented 6 years ago

my implementation below doesn't work. I collect all the data in onPause() but when I leave the app, close it, and relaunch it then the data isn't loaded. When is the data saved ?

@AutoRestore String autoRestore_dob;
@AutoRestore Integer autoRestore_state_pos;
@AutoRestore Integer autoRestore_gender_pos;
@AutoRestore Boolean autoRestore_tobacco_yes;
@AutoRestore Boolean autoRestore_tobacco_no;

@Override
public void onCreate(Bundle inState) {
    super.onCreate(inState);
    ButterKnife.bind(this);
    // code
    restoreState();
}

private void restoreState(){
    if (autoRestore_dob != null) dob.setText(autoRestore_dob);
    if (autoRestore_state_pos != null) stateSpinner.setSelection(autoRestore_state_pos);
    if (autoRestore_gender_pos != null) genderSpinner.setSelection(autoRestore_gender_pos);
    if (autoRestore_tobacco_yes != null) radioYes1.setChecked(autoRestore_tobacco_yes);
    if (autoRestore_tobacco_no != null) radioNo1.setChecked(autoRestore_tobacco_no);
}

@Override
protected void onPause() {
    saveState();
    super.onPause();
}

private void saveState(){
    autoRestore_dob = dob.getText().toString();
    autoRestore_state_pos = stateSpinner.getSelectedItemPosition();
    autoRestore_gender_pos = genderSpinner.getSelectedItemPosition();
    autoRestore_tobacco_yes = radioYes1.isChecked();
    autoRestore_tobacco_no = radioNo1.isChecked();
}
abiemann commented 6 years ago

oh I see now... the library generates the onSaveInstanceState() and onRestoreInstanceState(). Unfortunately, onSaveInstanceState() is unreliable - it's not always triggered, but is typically triggered on phone rotations. I need it more reliable... I need the data saved whenever onPause() is called. Right now, a person can press the HOME button, close the app in the list of apps, and the typed-in data is not saved

PrototypeZ commented 6 years ago

Thanks for your suggestion! And yes I wrote this lib to help app data survive on orientation change and situation that app is killed by system unexpectedly when it is brought to background. It isn't a data persistent library by design. But I think your suggestion is interesting, I'm considering to add this functionality to later versions, thanks :)

PrototypeZ commented 5 years ago

After consideration, I think SaveState is only a simple tool, and adding data persistent function to it would make it a little complex, so temporary I am not planning to change it. If more guys ask me for this feature then reopening this issue is ok.