FragmentPagerAdapter and FragmentStatePagerAdapter are both adapters used to adapt data to ViewPager in Android development. There are significant differences between the two in the way they use and manage Fragments. When switching Fragments, FragmentPagerAdapter does not destroy the Fragment, but only calls the detach method in the transaction. Therefore, the view of the Fragment will be destroyed, but the instance of the Fragment will remain in the FragmentManager. The Fragments created in this way will never be destroyed, which is suitable for some static Fragments, such as a group of tabs. This may also cause the application to occupy too many resources when the number of Fragments is large. When switching between different fragments, FragmentStatePagerAdapter will destroy the fragments that are no longer needed. Before destroying the fragment, the state information of the fragment (saved by the onSaveInstanceState(Bundle) method) will be saved in the Bundle. After switching back to the original page, the saved state can be used to restore and generate a new fragment. It is suitable for scenarios with a large number of pages or where fragments need to be loaded and destroyed dynamically, and can effectively manage memory usage. FragmentPagerAdapter source code In the instantiateItem method, the main thing is to add the Fragment to the FragmentManager. If it is not added to the FragmentManager, the add operation is performed, and if it is added to the FragmentManager, only the attach operation is performed. In the destroyItem method, only the detach operation is performed. The detach operation does not destroy the Fragment, and the Fragment is still managed by the FragmentManager. FragmentStatePagerAdapter source code
FragmentStatePagerAdapter stores Fragments through an mFragments array and stores the state of Fragments when they are destroyed through the mSavedState array. The Fragment obtained through position may be empty (recycled). If it is empty, the getItem method will be called again to recreate a new Fragment, and then the state stored in mSavedState will be reassigned to the new Fragment to achieve the effect of Fragment recovery.
When the item is not visible on the page, the state of the Fragment will be saved to mSavedState first, and the Fragment instance will be destroyed. SummarizeFragmentPagerAdapter and FragmentStatePagerAdapter are both adapters used with ViewPager in Android development. Similarities:
Differences: (1) Fragment destruction strategy:
(2) State preservation and restoration:
The main difference between FragmentPagerAdapter and FragmentStatePagerAdapter is how they manage the Fragment lifecycle. The former retains the Fragment instance, which is suitable for scenarios with a small number of Fragments and do not need to be created and destroyed frequently; the latter destroys the Fragment when it is no longer needed, which is suitable for scenarios with a large number of Fragments or those that need to be loaded and destroyed dynamically. |
<<: Exploration and practice of Ctrip Hotel's unified cloud mobile phone platform
>>: The key to optimizing Bitmap memory usage: image resolution, folder storage and loading strategy
At the bottom of the spiral channel, there will b...
Those preparing for CET-8, those preparing for po...
When renovating or remodeling a house, many peopl...
Traveling during the holidays is a good opportuni...
How did you come up with the title? Do you receiv...
A few days ago, a netizen in Chongqing posted a m...
Speaking of "sepsis", you may feel unfa...
How much does it cost to join a fresh food mini p...
Please call our 24-hour customer service hotline ...
Social zero refers to the community spread of the...
1. WHAT (1) What is a community ? Just like makin...
When a product reaches a certain stage of develop...
Target: A national wedding photography chain clie...
How to purchase the video of African children hol...
" Exercise-induced sudden death " refer...