android - Single Activity with Multiple Fragments and Screen Orientation -
i'm dealing issue android & it's re-creation cycle on screen rotation:
i have 1 single activity , lots of fragments (support-v4) within. example, login it's on single activity fragment, when logs-in app changes it's navigation behavior , uses multiple fragments, did this, because passing data between fragment fragment b it's way easier passing data between activity activity b.
so issue it's presented when rotate device, on first approach, initial fragment loaded, happen, if user it's on page 15 , rotates it's device, return fragment 1 , give bad user-experience. set fragments retain instance , added on mainactivity on create:
@override protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); setcontentview(r.layout.main_layout); initbackstackmanager(); initcontrollers(); maydownloaddata(); settitle(); if(savedinstancestate == null){ addareafragment(); } }
now, first fragment not loaded after screen orientation change, if try make fragment transaction, says can not perform fragmenttransaction.commit() after onsaveinstancestate(), there way handle this? or really need use multiple activities fragment embedded within?
thank much!
edited forgot add happens on specific fragment... example have following fragment flow:
areafragment -> waiterselectionfragment -> waiteroptionsfragment.
if i'm in areafragment , rotate device can still add/replace fragments , nothing happens, no error it's being thrown. if i'm on waiterselectionfragment no error happens too. but, if i'm on waiteroptionsfragment error it's being thrown. waiterselectionfragment has following structure:
- linearlayout
- fragmenttabhost
inside fragmenttabhost there fragments, , that's error it's happening. might wonder why fragmenttabhost? easy, customer wants app show tabbar, if use native android tabs tabs rearranged actionbar when on landscape position.
edit 2
i've used method provided @aj macdonald, no luck far. have current fragment being saved @ onsaveinstancestate(bundle)
method , restore fragment on onrestoreinstancestate(bundle)
method on android activity, recover back button , current fragment when third fragment error still occurs. i'm using viewpager holds 4 fragments, causing issue? on section of app happens. i've 4 (main workflow) fragments, on first, second , third fragment no error it's being presented, on viewpager part.
give each of fragments unique tag.
in activity's onsaveinstancestate
, store current fragment. (this easiest if keep variable automatically updates every time fragment changes.)
in activity's oncreate
or onrestoreinstancestate
, pull tag out of saved bundle , start new fragment of type.
public static final int fragment_a = 0; public static final int fragment_b = 1; private int currentfragment; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); //other stuff if(savedinstancestate == null){ addareafragment(); currentfragment = fragment_a; }else{ currentfragment = savedinstancestate.getint("currentfragment"); switch(currentfragment){ case fragment_a: addareafragment(); break; case fragment_b: addfragmentb(); } } } // when switch fragment fragment b: currentfragment = fragment_b; @override public void onsaveinstancestate(bundle savedinstancestate) { savedinstancestate.putint("currentfragment", currentfragment); super.onsaveinstancestate(savedinstancestate); }
Comments
Post a Comment