I need to save my filters after closing Activity
. I'm using onSaveInstanceState()
to achieve it.
protected void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putInt("age", sbAge.getProgress());
savedInstanceState.putInt("age", spMyStatus.getSelectedItemPosition());
if(rbMaleMe.isChecked())
savedInstanceState.putInt("sex", 1);
else if(rbFemaleMe.isChecked())
savedInstanceState.putInt("sex", 2);
}
`
In onRestoreInstanceState()
method:
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
sbAge.setProgress(savedInstanceState.getInt("age"));
spMyStatus.setSelection(savedInstanceState.getInt("status"));
if(savedInstanceState.getInt("sex") == 1)
rbMaleMe.isChecked();
else if (savedInstanceState.getInt("sex") == 2)
rbFemaleMe.isChecked();
}
In onCreate()
method:
if (savedInstanceState != null)
{
savedInstanceState.getInt("age");
savedInstanceState.getInt("sex");
savedInstanceState.getInt("status");
}
but it doesn't work. Just shows empty views when I close and restart the activity.
onClick()
:
switch (v.getId()) {
case R.id.btnSaveInfo:
onStop();
finish();
break;
}
onSaveInstanceState()
is called when the OS kills your Activity
(to reclaim resources for example). It will not be called when you explicitly call finish()
.
If you would still like to persist your values, you could use SharedPreferences
.
(on a side note, do not call onStop()
explicitly)
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.