Reputation: 12467
After popping the backstack, my Fragments are not being garbage collected? Their onStop()
methods are called, but not their onDestroyView()
or onDestroy()
methods.
How can I ensure that their onDestory()
methods are called?
I made the mistake of making a "God Activity", and all my fragments .replace()
a single container FrameLayout
. Let's say I have four fragments, FragA
, FragB
, FragC
, and FragD
, with a backstack that looks something like this:
FragA
-> FragB
-> FragC
-> FragD
FragD is the only visible fragment. Then, I call .
popBackstack()`
FragA
-> FragB
-> FragC
=!= FragD
FragD
has been detached, and FragC
is now visible to the user. When try to instantiate a new FragD
(from FragC
), it's created, and there are two instances of FragD
on my heap. And the first FragD
instance's onDestroyView()
is not called.
How do I fix this?
EDIT: here's an example of how I am currecntly replacing fragments in my UI:
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, new SearchScreenFragment())
.addToBackStack(null)
.commit();
EDIT2: onDetach()
isn't being called either.
Upvotes: 1
Views: 610
Reputation: 12467
I should have been using this: getChildFragmentManager()
:
From https://stackoverflow.com/a/23500935/891242
Upvotes: 2