Having searched regarding this issue beforehand, I can find many discussions regarding dynamically adding and removing selected Fragment
s from a ViewPager
However, I don't like the fact that those Fragments' end of life methods aren't called as I can't do any cleanup within them, and it just doesn't seem 'right'.
They should get cleaned up when the activity is destroyed, if that is not too late for you (e.g., heap issues).
In other words, when I know that the ViewGroup is no longer in used, I would perform FragmentTransactions somewhere (perhaps in the FragmentPagerAdapter) to remove those Fragments.
You did not execute the transactions to put the fragments there. Hence, you cannot readily execute the transactions to remove the fragments. If you switch to FragmentStatePagerAdapter
, and call setAdapter(null)
, it should cause all existing fragments in the pager to be destroyed, by my reading of the source code. FragmentPagerAdapter
never uses remove()
, but FragmentStatePagerAdapter
does, from its destroyItem()
method, and all extant fragments are destroyed via destroyItem()
when a new adapter (or null
) is supplied to setAdapter()
.