android - Saving the tick in the checkbox isn't working after quiting and then returning to app -
android - Saving the tick in the checkbox isn't working after quiting and then returning to app -
hi have got tick box want user tick , and maintain tick saved when user leaves app or navigate different activity , returning. code of have got sofar doesn't seem working
@override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.test_ticking, container, false); ch = (checkbox) rootview.findviewbyid(r.id.tickbox1); ch.setonclicklistener(new view.onclicklistener() { sharedpreferences pref = getactivity().getsharedpreferences("pref",0); @override public void onclick(view v) { // todo auto-generated method stub if(pref.getboolean("check", false)) { ch.setchecked(false); pref.edit().putboolean("check", false).commit(); } else { ch.setchecked(true); pref.edit().putboolean("check", true).commit(); } } public void onresume() { }{ if(pref.getboolean("check", true)) { ch.setchecked(false); } else { ch.setchecked(false); } } }); homecoming rootview; } }
can 1 assist me in issue. don't think can hard work?
to store preferences this. instead of using onclicklistener utilize oncheckedchangelistener:
ch.setoncheckedchangelistener(new oncheckedchangelistener() { @override public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) { sharedpreferences pref = getactivity().getsharedpreferences("pref",0); if (ischecked) { pref.edit().putboolean("check", true).commit(); } else { pref.edit().putboolean("check", false).commit(); } } });
your onresume method should this:
@override public void onresume() { super.onresume(); sharedpreferences pref = getactivity().getsharedpreferences("pref",0); if(pref.getboolean("check", false)) { ch.setchecked(true); } else { ch.setchecked(false); } }
android
Comments
Post a Comment