android - onSaveInstanceState not working -
i trying save type of map user selected through menu type of map remains if device rotated or activity suspended few moments. did doesn't seem work. please can tell me doing wrong?
private int maptypeselected; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); if (savedinstancestate == null){ maptypeselected = googlemap.map_type_normal; } else { maptypeselected = savedinstancestate.getint("the_map_type",googlemap.map_type_normal); } }
my main menu has more choices added here relevant ones map type:
@override public boolean onoptionsitemselected(menuitem item) { switch (item.getitemid()) { case r.id.change_map_type: toast.maketext(getapplicationcontext(), "change map type", toast.length_long).show(); return true; case r.id.map_type_normal: maptypeselected = googlemap.map_type_normal; mmap.setmaptype(maptypeselected); return true; case r.id.map_type_satellite: maptypeselected = googlemap.map_type_satellite; mmap.setmaptype(maptypeselected); return true; default: return super.onoptionsitemselected(item); } }
then @ end of activity added two:
@override protected void onsaveinstancestate(bundle outstate) { super.onsaveinstancestate(outstate); outstate.putint("the_map_type", maptypeselected); } @override protected void onrestoreinstancestate(bundle savedinstancestate) { super.onrestoreinstancestate(savedinstancestate); savedinstancestate.get("the_map_type"); }
i sure stupid doing wrong cannot seem see what. there no error , working fine on rotation map type changes normal. in advance help!
try one..
private int mmaptype; private boolean mrestoremap; @override protected void onsaveinstancestate(bundle outstate) { super.onsaveinstancestate(outstate); outstate.putint("the_map_type", mmap.getmaptype()); } @override protected void onrestoreinstancestate(bundle savedinstancestate) { super.onrestoreinstancestate(savedinstancestate); mmaptype = savedinstancestate.getint("the_map_type", -1); mrestoremap = (mmaptype != -1); } @override public void onmapready(googlemap map) { mmap = map; if(mrestoremap){ mmap.setmaptype(mmaptype); } }
Comments
Post a Comment