java - Orientation Change Causes Series of Fragments in Activity Disappear and Do Not Apply Save Instance State -
so hit roadblock in development project.
this time involves fragments regard changing orientation , persisting saved instance states. please pardon me if involves 2 questions involve same problem catalyst orientation change. try detailed possible please bear me.
i have bunch of fragments using single activity host. in mind, fragments grouped sections although still modular/reusable code.
so have 3 fragments hosted inside single fragment implements navigation drawer. logical flow goes this:
frag1 -> (press next) -> frag2 -> (press next) -> frag3
the creation of frag2 , frag3 calls addtobackstack() in committing transaction in fragmentmanager. problem when in frag2 or frag3 , change device orientation, fragments disappear , show frag1.
the other problem when in frag1, type text in edittext, , change orientation, text disappears despite implementing onsaveinstancestate(bundle bundle).
here relevant code snippets:
code snippet creating frag1 in activity.java
@override public void onnavigationdraweritemselected(int position) { // init fragment (with default fragment, not null) rootfragment = placeholderfragment.newinstance(position + 1); // position number navigation sidebar starts 0. // since position starts 0, add 1 match section number // implemented in {@link #onsectionattached()} switch (position) { case 0: // other section rootfragment = placeholderfragment.newinstance(position + 1); break; case 1: // frag1 rootfragment = new addpointsfragment().newinstance(position + 1, ""); break; case 2: // other section rootfragment = new checkpointsfragment().newinstance(position + 1, ""); break; default: break; } // update main primary content replacing fragments fragmentmanager fragmentmanager = getfragmentmanager(); // clear fragments previous section stack fragmentmanager.popbackstackimmediate(null, fragmentmanager.pop_back_stack_inclusive); // replace added fragments in container , replace new fragment // don't add stack since these serve "root" fragments fragmentmanager.begintransaction() .replace(r.id.container, rootfragment, rootfragment.getclass().getname()) .commit(); }
code snippet creating frag2 in activity.java
public void startaddpointssuccessfragment(int sectionnumber, string cardno, int points) { // create fragment , give arguments // since not major fragment, // used major parent fragment (addpointsfragment) // section number follow action bar title addpointssuccessfragment addpointssuccessfragment = new addpointssuccessfragment().newinstance(sectionnumber, cardno, points); // replace whatever in container view fragment, // , add transaction stack user can navigate fragmentmanager fragmentmanager = getfragmentmanager(); fragmentmanager.begintransaction() .replace(r.id.container, addpointssuccessfragment) .addtobackstack(addpointssuccessfragment.class.getname()) .commit(); }
code snippet creating frag3 in activity.java
public void onokbuttonaddpointssuccessfragmentinteraction(int sectionnumber, string cardno, int points) { int minimumpoints = 10; if (points < minimumpoints) { // go previous fragment (addpointsfragment) // simulating press host activity. this.onbackpressed(); } else { // create fragment , give arguments // since not major fragment, // used major parent fragment (addpointsfragment) // section number follow action bar title redeemrewardfragment redeemrewardfragment = new redeemrewardfragment().newinstance(sectionnumber, cardno, points); // replace whatever in container view fragment, // , add transaction stack user can navigate fragmentmanager fragmentmanager = getfragmentmanager(); fragmentmanager.begintransaction() .replace(r.id.container, redeemrewardfragment) .addtobackstack(redeemrewardfragment.class.getname()) .commit(); } }
regarding fragments disappearing upon change in orientation, still not able come possible solution yet.
regarding persistence of saved instance state upon change in orientation, tried implementing onsaveinstancestate(bundle bundle) inside frag1 class this:
code snippet addpointsfragment.java
edittext cardnoedittext; @override public void onsaveinstancestate(bundle bundle) { super.onsaveinstancestate(bundle); bundle.putint(arg_section_number, msectionnumber); bundle.putstring(arg_card_no, cardnoedittext.gettext().tostring()); } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); if (getarguments() != null) { msectionnumber = getarguments().getint(arg_section_number); mcardno = getarguments().getstring(arg_card_no); } if (savedinstancestate != null) { msectionnumber = savedinstancestate.getint(arg_section_number); mcardno = savedinstancestate.getstring(arg_card_no); } } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view view = inflater.inflate(r.layout.fragment_add_points, container, false); cardnoedittext = (edittext) view.findviewbyid(r.id.cardnoedittext); cardnoedittext.settext(mcardno); button entercardnobutton = (button) view.findviewbyid(r.id.entercardnobutton); entercardnobutton.setonclicklistener(this); button scancardnobutton = (button) view.findviewbyid(r.id.scancardnobutton); scancardnobutton.setonclicklistener(this); return view; }
i tried running debugger in code regarding failed persistence during change of orientation, , upon inspection onsaveinstancestate(bundle bundle) work , able pass data edittext. however, found activity calls onnavigationdraweritemselected(int position) when changing orientation, can see in code inside it, wipes fragmentmanager of frag1 , replaces new frag1 not have value of cardno yet.
i guess underlying question here best way of implementing onsaveinstancestate(bundle bundle) in scenario? tried implementing inside fragments may have made mistake? or should implement in activity saving instance of existing fragments?
thanks in advance answer. :)
very rough idea
in oncreate ?? or oncreateview ???
if(savedinstancestate == null){ }else{ int positionx = savedinstancestate.getint("myposition"); //setfragment myposition ??!!??!!?? myedittext.settext(savedinstancestate.getstring("myedittext")); //set text edittext ??!!??!!??!! } @override public void onsaveinstancestate(bundle savedinstancestate) { super.onsaveinstancestate(savedinstancestate); // save ui state changes savedinstancestate. // bundle passed oncreate if process // killed , restarted. savedinstancestate.putint("myposition", position); savedinstancestate.putstring("myedittext", myedittext.gettext().tostring()); } @override public void onrestoreinstancestate(bundle savedinstancestate) { super.onrestoreinstancestate(savedinstancestate); // restore ui state savedinstancestate. // bundle has been passed oncreate. int positionx = savedinstancestate.getint("myposition"); //setfragment myposition ??!!??!!?? myedittext.settext(savedinstancestate.getstring("myedittext")); //set text edittext ??!!??!!??!! }
Comments
Post a Comment