I am developing an application. There are many checkbox in one page. I want to save the state of CheckBox when I exit page. I use SharedPreferrences to save. The code is like this:
checkbox1.setOnCheckedChangeListener(new OnCheckedChangeListener(){
public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
// TODO Auto-generated method stub
Editor editor = getSharedPreferences("syllabus", 0).edit();
editor.putBoolean("cbx1_ischecked", isChecked);
editor.commit();
}
});
checkbox2.setOnCheckedChangeListener(new OnCheckedChangeListener(){
public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
// TODO Auto-generated method stub
Editor editor = getSharedPreferences("syllabus", 0).edit();
editor.putBoolean("cbx2_ischecked", isChecked);
editor.commit();
}
});
When I leave this page, and back again. All the checkbox state becomes not selected as default. why?
You are saving vallues but you have to restore them you your activity is started like this :