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
1. What is inner scrolling layout? The so-called ...
This issue introduces a white mica specimen from ...
In the process of dividing up channel resources a...
There is an embarrassment called foot odor. Espec...
It is now March, and the official release of Andr...
Beijing time, April 28, according to the technolo...
American telecommunications giant AT&T and Ti...
Content: Changsha New Tea website appointment arr...
Yes, you read it right. A group of Russian girls ...
Editor's Note: How to build a component libra...
Your browser does not support the video tag Video...
After overcoming many hurdles in its acquisition ...
I have three Tik Tok communities , which contain ...
ExoPlayer Introduction ExoPlayer is an open sourc...