java - Android saving state of checkbox if exit app -
i have 5 checkboxs , when clicked set boolean in abstract class true or false using oncheckedchangelistener. example:
checkbox.setoncheckedchangelistener(new oncheckedchangelistener(){ @override public void oncheckedchanged(compoundbutton buttonview,boolean ischecked) { if(ischecked) { checkboxdata.checked = true; } else if(!ischecked) { checkboxdata.checked = false; } } });
however when leave app , return checkboxes not clicked anymore, yet boolean values still true. how can make app remember check boxes clicked.? should check boolean values in main activity oncreate , set checkboxs checked or not or there better/faster way make app remember checkboxs states?
just added in main activities oncreate method:
if(checkboxdata.checked) { checkbox checkbox = (checkbox) findviewbyid(r.id.checkbox); checkbox.setchecked(true); }
but nothing
when activity
destroyed
check checkbox state if true , , add checkbox state in shared preference
@override public void ondestroy() { if(checkboxdata.checked){ sharedpreferences prefs = getsharedpreferences("private preference", context.mode_private); prefs.edit() .putboolean("checkboxdata",true) .commit(); } super. ondestroy(); }
and when activity
launched back, in oncreate
check checkbox state in preference , if true enable checkbox
sharedpreferences prefs = getsharedpreferences("private preference", context.mode_private); boolean ischecked = prefs.getboolean("checkboxdata",false); if(ischecked) checkboxview.setchecked(true)
Comments
Post a Comment