Android dynamic Fragment recreated several times on Screen orientation change, after onclick list selection -



Android dynamic Fragment recreated several times on Screen orientation change, after onclick list selection -

i working on next tutorial, has problems : recreates fragments after each screen rotation. fixed concerning titlesfragment class adding if(savedinstancestate == null) in quotevieweractivity:

mfragmentmanager = getfragmentmanager(); //added status if(savedinstancestate == null){ fragmenttransaction fragmenttransaction = mfragmentmanager .begintransaction(); fragmenttransaction.add(r.id.title_fragment_container, mtitlesfragment); fragmenttransaction.commit(); }

it fixed fragment titlesfragment fragment quotefragment still recreating on each screen orientation alter because in tutorial fragment created in onclick event:

@override public void onlistselection(int index) { if (!mdetailsfragment.isadded()) { fragmenttransaction fragmenttransaction = mfragmentmanager .begintransaction(); fragmenttransaction.add(r.id.quote_fragment_container, mdetailsfragment); fragmenttransaction.addtobackstack(null); fragmenttransaction.commit(); mfragmentmanager.executependingtransactions(); } if (mdetailsfragment.getshownindex() != index) { mdetailsfragment.showindex(index); } }

note setretaininstance(true) set in both fragment's oncreate().

i tried add together checking didn't prepare it:

@override public void onlistselection(int index) { //added next 2 lines fragment f = mfragmentmanager.findfragmentbyid(r.id.quote_fragment_container); if(f == null) //=============================== if (!mdetailsfragment.isadded()) { fragmenttransaction fragmenttransaction = mfragmentmanager .begintransaction(); fragmenttransaction.add(r.id.quote_fragment_container, mdetailsfragment); fragmenttransaction.addtobackstack(null); fragmenttransaction.commit(); mfragmentmanager.executependingtransactions(); } if (mdetailsfragment.getshownindex() != index) { mdetailsfragment.showindex(index); } }

==> recreates fragment each time rotate screen , duplicates existing menus (explained in snapshot):

what doing wrong , best practice prepare this? thanks!

this line won't find fragment you're trying find:

fragment f = mfragmentmanager.findfragmentbyid(r.id.quote_fragment_container);

you provided container (layout) id. method can used find fragments inflated xml layout.

if want manage fragments code, utilize tag. add together fragment using fragmenttransaction.add(int containerviewid, fragment fragment, string tag). providing tag can later find fragment using fragmentmanager.findfragmentbytag(string tag). it's thought create tag kind of static final string constant, making automatic refactoring breeze.

you may interested method fragmenttransaction.replace(int containerviewid, fragment fragment, string tag) - makes fragment replacement easier.

android android-fragments screen-orientation android-listfragment

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -