android - Replacing a fragment in a FrameLayout does not remove the existing fragment from the fragmentManager -
i adding fragment dynamically so:
fragmentmanager fm = getchildfragmentmanager(); mfragmentmanager.begintransaction().replace(r.id.learn_detail, detailfragment, learn_fragment_threats).commit(); mfragmentmanager.executependingtransactions();
i call same code when add different fragment r.id.learn_detail (which framelayout)
the fragment replaced fine, have noticed old fragment still exists in memory.
calling mfragmentmanager.getfragments
returns list gets bigger , bigger each time call replace
. expected replace
destroy old fragment , add new fragment in framelayout. thoughts?
edit - problem was creating new fragment, no matter what. complete method ended looking this:
private learnpagefragment getpagefragment(learnpageitem item) { learnpagefragment learnpagefragment = (learnpagefragment) mfragmentmanager.findfragmentbytag(learn_fragment_page); if (learnpagefragment == null) { learnpagefragment = learnpagefragment.newinstance(item); } else { //destroy view , update item data mfragmentmanager.begintransaction().remove(learnpagefragment).commit(); mfragmentmanager.executependingtransactions(); learnpagefragment.getarguments().putserializable(learnpagefragment.k_content, item); } return learnpagefragment; }
i accepted answer put me on right track. shouldn't trying manually remove fragments fragment manager.
when fragmentmanager
removes fragment ui, fragment still being managed fragmentmanager
. aside finishing activity, don't know of way tell fragmentmanager
"forget" fragment.
the expectation reuse fragments have created rather instantiating new 1 on , over. example, calling fragmenttransaction.add()
tag argument, calling fragmentmanager.findfragmentbytag()
reference fragment.
Comments
Post a Comment