android - Using saveinstancestate for saving a lot of checkbox states -


i know there lot of documentation using save instance state confused on how implement case. have custom action bar activities custom buttons take previous , next pages. when navigate via these buttons, want able save state of of checkboxes. i'm 90% sure i'm doing wrong , wonder if there better way considering amount of lines i'm using. code in first activity , when clicked custom next button , previous button return it, it's not saving state:

boolean bprearrival_1, bprearrival_2, bprearrival_3, bprearrival_4, bprearrival_5,         bprearrival_6, bprearrival_7, bprearrival_8, bprearrival_9, bprearrival_10,         bprearrival_11, bprearrival_12;  @override public void onsaveinstancestate(bundle savedinstancestate) {     super.onsaveinstancestate(savedinstancestate);     savedinstancestate.putboolean("prearrival_1", checkboxlist.get(0).ischecked());     savedinstancestate.putboolean("prearrival_2", checkboxlist.get(1).ischecked());     savedinstancestate.putboolean("prearrival_3", checkboxlist.get(2).ischecked());     savedinstancestate.putboolean("prearrival_4", checkboxlist.get(3).ischecked());     savedinstancestate.putboolean("prearrival_5", checkboxlist.get(4).ischecked());     savedinstancestate.putboolean("prearrival_6", checkboxlist.get(5).ischecked());     savedinstancestate.putboolean("prearrival_7", checkboxlist.get(6).ischecked());     savedinstancestate.putboolean("prearrival_8", checkboxlist.get(7).ischecked());     savedinstancestate.putboolean("prearrival_9", checkboxlist.get(8).ischecked());     savedinstancestate.putboolean("prearrival_10", checkboxlist.get(9).ischecked());     savedinstancestate.putboolean("prearrival_11", checkboxlist.get(10).ischecked());     savedinstancestate.putboolean("prearrival_12", checkboxlist.get(11).ischecked()); }  @override protected void onrestoreinstancestate(bundle savedinstancestate) {     super.onrestoreinstancestate(savedinstancestate);     bprearrival_1 = savedinstancestate.getboolean("prearrival_1");     bprearrival_2 = savedinstancestate.getboolean("prearrival_2");     bprearrival_3 = savedinstancestate.getboolean("prearrival_3");     bprearrival_4 = savedinstancestate.getboolean("prearrival_4");     bprearrival_5 = savedinstancestate.getboolean("prearrival_5");     bprearrival_6 = savedinstancestate.getboolean("prearrival_6");     bprearrival_7 = savedinstancestate.getboolean("prearrival_7");     bprearrival_8 = savedinstancestate.getboolean("prearrival_8");     bprearrival_9 = savedinstancestate.getboolean("prearrival_9");     bprearrival_10 = savedinstancestate.getboolean("prearrival_10");     bprearrival_11 = savedinstancestate.getboolean("prearrival_11");     bprearrival_12 = savedinstancestate.getboolean("prearrival_12"); }  @override protected void onresume() {     super.onresume();     checkboxlist.get(0).setchecked(bprearrival_1);     checkboxlist.get(1).setchecked(bprearrival_2);     checkboxlist.get(2).setchecked(bprearrival_3);     checkboxlist.get(3).setchecked(bprearrival_4);     checkboxlist.get(4).setchecked(bprearrival_5);     checkboxlist.get(5).setchecked(bprearrival_6);     checkboxlist.get(6).setchecked(bprearrival_7);     checkboxlist.get(7).setchecked(bprearrival_8);     checkboxlist.get(8).setchecked(bprearrival_9);     checkboxlist.get(9).setchecked(bprearrival_10);     checkboxlist.get(10).setchecked(bprearrival_11);     checkboxlist.get(11).setchecked(bprearrival_12);  } 

for less code use

void savechecliststateinpreference(){  sharedpreferences preferences =  preferencemanager.getdefaultsharedpreferences(this); string prearrival_prefix = "prearrival_"; string checkvalue = ""; sharedpreferences.editor editor =  preferences.edit();  //total length of checkboxes for(int = 0 ; i< 11 ; i++){     checkvalue = prearrival_prefix + string.valueof(i+1);//because no 0 based started     preferences.putboolean(checkvalue,checkboxlist.get(i).ischecked()) } editor.apply(); 

}

and recover same way


Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -