I’ve used this code to get one checkbox saved for when the user returns, but I need to have many checkboxes throughout the application. I’m sure the best way isn’t to copy and paste this code, but can’t seem to find what it is.
What would I add or change to make this work with say 10 or more checkboxes?
@Override
public void onPause() {
super.onPause();
save(mCheckBox.isChecked());
}
@Override
public void onResume() {
super.onResume();
mCheckBox.setChecked(load());
}
private void save(final boolean isChecked) {
SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("check", isChecked);
editor.commit();
}
private boolean load() {
SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
return sharedPreferences.getBoolean("check", false);
}
Or create a schema of your own (SQLite, etc) and persist that way. In any case, every unique checkbox needs to have a unique id in the persistent store.