Hello,
Welcome to our Microsoft Q&A platform!
If you create custom FragmentPagerAdapter
, you do not need to override the DestroyItem
to recycle fragments manually.
The default for the setOffScreenPageLimit
should already be one. This means that Android will destroy old fragments when memory runs low. As long as you do not have a memory issue, just leave it be.
If you want to recycle other disappeared Fragments automatically, you can extend FragmentStatePagerAdapter, Entire fragment may be destroyed, only keeping the saved state of that fragment. This allows the pager to hold on to much less memory associated with each visited page as compared to FragmentPagerAdapter at the cost of potentially more overhead when switching between pages.
Here is a document about achieving a custom FragmentPagerAdapter in Xamarin. You can refer to it.
If FragmentStateAdapter is deprecated,
Switch to androidx.viewpager2.widget.ViewPager2 and use androidx.viewpager2.adapter.FragmentStateAdapter instead.
If you want to use ViewPager2, you need to install Xamarin.AndroidX.ViewPager2nuget package:
Best Regards,
Leon Lu
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.