android - How to avoid multiple instances of fragments in Activity after app is killed and resumed? -


i have app home screen has 2 fragments (for now) , navigation drawer. load fragment (explore) on startup , load fragment b when clicked. on, show , hide fragments. it's faster recreating fragments on every click , fragment takes time load.

i've noticed when go fragment b , go activity (let's call activity 2) there , leave app , wait killed (or crazy change device language), , come same activity, it's still there. when press go fragment b, (50% of times) fragment b drawn on fragment a. on clicking fragment in drawer, fragment appears fine, on clicking fragment b, there's instance of fragment , on top of fragment b.

i've spent more 2 days on problem , got nowhere.

here's code selecting fragment:

private void selectitem(int position, boolean addexplorefragment) {     log.d(tag, "selectitem: " + position);      fragmenttransaction fragmenttransaction = getsupportfragmentmanager().begintransaction();     fragmenttransaction.settransition(fragmenttransaction.transit_fragment_open);      //add explore fragment - called on app startup, when app killed , resumed results in 2 explore fragments      if (addexplorefragment){          fragmenttransaction.replace(r.id.content_frame, mexplorefragment, explore_fragment_tag);         log.d(tag, "replaced frame , added "+ mfragmenttags[position]);      } else {         //add fragment first time         if (getsupportfragmentmanager().findfragmentbytag(mfragmenttags[position]) == null && position != 0) {             fragmenttransaction.add(r.id.content_frame, mfragments[position], mfragmenttags[position]);             log.d(tag, "added fragment: "+ mfragmenttags[position]);         }         //shows , hides fragments         (int = 0; < mfragments.length; i++) {             if (i == position) {                 fragmenttransaction.show(mfragments[i]);                 log.d(tag, "showing fragment: "+ mfragmenttags[i]);             } else {                 if (getsupportfragmentmanager().findfragmentbytag(mfragmenttags[i]) != null) {                     fragmenttransaction.hide(mfragments[i]);                     log.d(tag, "hid fragment: "+ mfragmenttags[i]);                 }             }         }      }      fragmenttransaction.commit();     //not null check calling selectitem(0) before loading drawer     if (mdrawerlist != null){         mdrawerlist.setitemchecked(position, true);     } } 

i know sure, explore fragment getting created twice , 2 instances behave independently of each other (just sharing).

i'm lost next. issue can reproduced on low end devices on device nexus 4 (my test device), issue can reproduced changing device language.

has got ideas this? if addexplorefragment block doesn't called when there explorefragment, issue solved, think, i've been unable so. also, tried removing fragments , adding explorefragment same thing happens (50% of times).

thanks! , sorry long post, felt should share details.

update: when change device language , come app on activity 2 , go home activity, has fragment b open good, fragment recreated because it's heavy fragment , system removed memory. again, that's ok gets recreated if got removed system why recreated when it's not removed. believe it's code, on every 2nd attempt (without closing app) happens, 2 instances of heavy fragment a. out of ideas.

but shouldn't fragmenttransaction.replace remove added fragments , add explorefragment. it's not working that. neither fragment nor fragment b getting removed.

i found out new , rather odd me. when use fragmenttransaction.add, listeners have, draweritemclicklistener, on previous fragment, still active. , if use fragmenttransaction.commit.

so...i suspect when add method used, clicked on hidden button or hidden ui has event listener on previous fragment. don't of course , effect may confusing. yes, happened me , didn't understand why while.

for now, think easiest code fix use replace method instead of add. replace() makes listeners inactive. if works, can make better/elegant fix.

let me know happens....


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -