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
When interviewing optimizers, I always ask, "...
On November 6, DeepBlue Automotive's intellig...
After more than a month of stumbling around the lo...
Recently, DeepBlue S05, a new model under DeepBlu...
On November 17, Li Yuchun released a new song tha...
On October 14, as we all know, Apple's operat...
On July 15, a large number of netizens posted on ...
I don't know if you have ever had the experie...
【Bai Gu Jing】 Xue Han's Chan Theory Stock Adv...
[Malaysia, May 7, 2024] The Lamborghini Super Tro...
Channel , we all know that channel has a synonym:...
Nowadays, exercise has become a "compulsory ...
At the free blood donation vehicle in front of th...
Assuming you've read about RxJava and have ex...
There are eight planets in the solar system we li...