android - How to save instance state of selected radiobutton on menu -


i have options menu in toolbar radibutton item :

 <item     android:id="@+id/map_menu"     android:icon="@drawable/ic_layer"     android:orderincategory="102"     app:showasaction="always"     android:title="@string/action_settings">     <menu>         <group             android:id="@+id/map_types_group"             android:checkablebehavior="single" >             <item                 android:id="@+id/map_terrain"                 android:orderincategory="1"                 app:showasaction="ifroom"                 android:title="@string/map_terrain"/>             <item                 android:id="@+id/map_normal"                 android:orderincategory="2"                 android:checked="true"                 app:showasaction="ifroom"                 android:title="@string/map_normal"/>             <item                 android:id="@+id/map_hybrid"                 android:orderincategory="3"                 app:showasaction="ifroom"                 android:title="@string/map_hybrid"/>         </group>     </menu> </item> 

i want restore selected radiobutton when orientation change happened in onsaveinstancestate,onrestoreinstancestate can't understand how selected button radiogroup in options menu.

here working , tested example. code in place, no matter how many times rotate screen, selected item persist.

first, create these instance variables keep track of state of menu , have name preference saving in bundle:

private final static string menu_selected = "selected"; private int selected = -1; menuitem menuitem; 

the saveinstancestate() method should save off selected value:

@override public void onsaveinstancestate(bundle savedinstancestate) {     savedinstancestate.putint(menu_selected, selected);     super.onsaveinstancestate(savedinstancestate); } 

update selected item in onoptionsitemselected():

@override public boolean onoptionsitemselected(menuitem item) {      int id = item.getitemid();      if (id == r.id.action_settings) {         log.d("settings", "id: " + id);         return true;     }      if (id == r.id.map_terrain){         log.d("menuitem", "terrain id: " + id);         selected = id;         item.setchecked(true);         return true;     }      if (id == r.id.map_normal){         log.d("menuitem", "normal id: " + id);         selected = id;         item.setchecked(true);         return true;     }      if (id == r.id.map_hybrid){         log.d("menuitem", "hybrid id: " + id);         selected = id;         item.setchecked(true);         return true;     }      return super.onoptionsitemselected(item); } 

in oncreate(), load saved data if exists:

@override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);      if (savedinstancestate != null){         selected = savedinstancestate.getint(menu_selected);     } } 

and re-select selected item in oncreateoptionsmenu():

@override public boolean oncreateoptionsmenu(menu menu) {     getmenuinflater().inflate(r.menu.menu_main, menu);      if (selected == -1){         return true;     }      switch (selected){         case r.id.map_terrain:             menuitem = (menuitem) menu.finditem(r.id.map_terrain);             menuitem.setchecked(true);             break;          case r.id.map_normal:             menuitem = (menuitem) menu.finditem(r.id.map_normal);             menuitem.setchecked(true);             break;          case r.id.map_hybrid:             menuitem = (menuitem) menu.finditem(r.id.map_hybrid);             menuitem.setchecked(true);             break;     }      return true; } 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -