I have an application with checkboxes, How can i save them?
My layout is:
Override onSaveInstanceState(Bundle outState) and write which ones are checked into outState. Then you can see which ones are checked when the activity is create by extracting that information from savedInstanceState passed to onCreate(Bundle savedInstanceState)
For example,
@Override
protected void onSaveInstanceStat(Bundle b){
b.putBoolean("een",een.isChecked());
b.putBoolean("v52",v52.isChecked());
b.putBoolean("v53",v53.isChecked());
// ... etc
}
@Override
protected void onCreate (Bundle savedInstanceState){
// everything you have in your onCreate implementation
een.setChecked(b.getBoolean("een"));
v52.setChecked(b.getBoolean("v52"));
v53.setChecked(b.getBoolean("v53"));
// ... etc
}
IMPORTANT: There is no guarantee that 'onSaveInstanceState' will be called. So only use this if saving the ui state is for convenience for the user and not vital to functionality. User will be happier if you don't save data on their device, ie by using SharedPreference.
How about SharedPreferences? You can store and load the state and the name of the checkbox in a key:value format. How to use.
E. g. something like this:
// Init
SharedPreferences settings = getSharedPreferences("mysettings", 0);
SharedPreferences.Editor editor = settings.edit();
// Save
boolean checkBoxValue = v42.isChecked();
editor.putBoolean("v42", checkBoxValue);
editor.commit();;
// Load
v42.setChecked(settings.getBoolean("v42", false));
However you should implement more efficient way, probably a loop through all the checkboxes to read its name or id, which you can call before leaving the activity.
If you want to save simple data across sessions take a look at the documentation here: http://developer.android.com/guide/topics/data/data-storage.html specifically the shared preferences boolean storage would do the trick.
On resume you should get the values from the shared prefs and set the checkboxes using them. On pause you should get the values from the check boxes and put them into the shared prefs.
I also ran into this http://developer.android.com/reference/android/preference/CheckBoxPreference.html when looking at the HeloViews tutorial.
Just some reading around, might get round to giving you some code examples if I have time later. Hope this helps.