Programmatically removing a ViewPager, when are (or how can I ensure) contained Fragments are destroyed?

后端 未结 1 527
不知归路
不知归路 2021-02-10 04:19

Having searched regarding this issue beforehand, I can find many discussions regarding dynamically adding and removing selected Fragments from a ViewPager

相关标签:
1条回答
  • 2021-02-10 04:48

    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().

    0 讨论(0)
提交回复
热议问题