android - avoid a replaced fragment from calling the previous when screen is rotated -
hey guys im trying make activity has 1 framelayout changed when item in drawer selected, problem when rotate screen fragment replaced goes previous fragment. ex. opened app , shows me fragment try select fragment drawer fragment b when try rotate while im on fragment b goes fragment a. here code.
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); initviews(); passedfragment = this.getintent().getextras().getstring("fragmentclass"); if(savedinstancestate != null){ getsupportfragmentmanager().findfragmentbytag(fragment_tag); } switch (passedfragment){ case "com.serverus.oom.fragments.fragmentagency": fragmentclass = fragmentagency.class; menuitemreserve = mmenu.finditem(r.id.agency_menu_item); break; case "com.serverus.oom.fragments.fragmentservices2": fragmentclass = fragmentservices2.class; menuitemreserve = mmenu.finditem(r.id.services_menu_item); break; case "com.serverus.oom.fragments.fragmentcontactus": fragmentclass = fragmentcontactus.class; menuitemreserve = mmenu.finditem(r.id.contact_menu_item); break; default: fragmentclass = fragmentagency.class; break; } fragmentreplace(fragmentclass); } public void fragmentreplace(class fragmentclass) { try { fragment = (fragment) fragmentclass.newinstance(); } catch (exception e) { e.printstacktrace(); } // insert fragment replacing existing fragment fragmentmanager fragmentmanager = this.getsupportfragmentmanager(); fragmenttransaction ft = fragmentmanager.begintransaction(); ft.replace(r.id.flcontent, fragment, fragment_tag).addtobackstack(null); ft.settransition(fragmenttransaction.transit_fragment_close).commit(); }
thanks in advanced
can show code ? better..
maybe because don't store passedfragment in saveinstance , when rotate screen activity recreate itself, passedfragment contains null. "fragmentagency.class" (default value) set when rotate.
this code:
passedfragment = this.getintent().getextras().getstring("fragmentclass");
..return null, because can "fragmentclass" value when activity create @ startactivity(..).
check: http://developer.android.com/training/basics/activity-lifecycle/recreating.html see how use activity state save , load activity data.
Comments
Post a Comment