android - Saving Multiple Fragments State -
i implemented appcompatactivity used fragment transaction. 1 appcompatactivity like:
public class uiactivity extends appcompatactivity { ... }
the fragment can begin transaction, leading having more 1 fragment of same class in stack. (illustration)
public class uifragment extends fragment implements filechooserlistener { mybutton.setonclicklistener(){ // stuff, met condition , add fragment ( of uifragment), possibly inflating view, add previous fragment stack } }
this works fine except when implemented onsaveinstance,on restore instance inflate firstfragment while other lost. want resume user stopped , being able access fragment in stack when user press button.
implementation
in uiactivity
uifragment uif; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); overridependingtransition(r.anim.push_up_in, r.anim.push_up_out); setcontentview(r.layout.activity_ui); if (savedinstancestate == null) { uif = new uifragment(); bundle bundle = new bundle(); bundle.putserializable("step", getintent().getextras().getserializable("step")); bundle.putstring(uifragment.step_key, getintent().getextras().getstring(uifragment.step_key)); uif.setarguments(bundle); fragmenttransaction transaction = getsupportfragmentmanager().begintransaction(); transaction.add(r.id.content_frame, uif, "myfragment").commitallowingstateloss(); } } public void onsaveinstancestate(bundle outstate){ getsupportfragmentmanager().putfragment(outstate,"myfragment",uif); } public void onrestoreinstancestate(bundle instate){ uif = (uifragment)getsupportfragmentmanager().findfragmentbytag("myfragment"); }
snippet adds uifragment stack
public void addnewuifragment(){ uifragment uif= new uifragment (); bundle bundle = new bundle(); bundle.putserializable("step", step); bundle.putstring(uifragment.step_key, sdata); uif.setarguments(bundle); fragmenttransaction transaction = fragmentmanager.begintransaction(); transaction.addtobackstack(null); transaction.setcustomanimations(r.anim.slide_in, r.anim.slide_out, r.anim.back_slide_in, r.anim.back_slide_out); transaction.add(r.id.content_frame, uif, "myfragment").commitallowingstateloss(); }
Comments
Post a Comment