Difference Between Fragmentpageradapter and Fragmentstatepageradapter

What is the difference between FragmentPagerAdapter and FragmentStatePagerAdapter?

Like the docs say, think about it this way. If you were to do an application like a book reader, you will not want to load all the fragments into memory at once. You would like to load and destroy Fragments as the user reads. In this case you will use FragmentStatePagerAdapter. If you are just displaying 3 "tabs" that do not contain a lot of heavy data (like Bitmaps), then FragmentPagerAdapter might suit you well. Also, keep in mind that ViewPager by default will load 3 fragments into memory. The first Adapter you mention might destroy View hierarchy and re load it when needed, the second Adapter only saves the state of the Fragment and completely destroys it, if the user then comes back to that page, the state is retrieved.

Why should I use FragmentPagerAdapter instead of FragmentStatePagerAdapter?

What is the advantages of using FragmentPagerAdapter?

Speed, particularly when you have an intermediate number of pages: enough to easily hold in memory but beyond the handful that ViewPager wants to hold onto itself. With FragmentStatePagerAdapter, as the user navigates the pager, the adapter destroys some fragments and creates new ones. That takes time, both in terms of the direct Java code and in terms of the impact upon garbage collection. If you do not need that in some circumstance, why pay the price?

Difference FragmentPagerAdapter and PagerAdapter

The difference is that you can use Fragments inside a FragmentPageAdapter. If you want to have fragments that are going to be used in other parts of your code this is your Adapter.
Otherwise if you only want to have code that isn't going to be reused, you can use PagerAdapter.

FragmentPagerAdapter and FragmentStatePagerAdapter

I messed up in onPageSelectedMethod - I was forcing FragmentStatePagerAdapter to call getItem - which instantiates Fragment at least once - method whenever page is changed. That's why I complained about instantiating Fragment every time page is changed :)

Instead of getItem() I should call there my getFragmentAt() method, and the whole callback should look like that.

@Override
public void onPageSelected(int pageNumber) {
wizardActivity.stepFragment.setCurrentStepAndChangeText(pageNumber);

if(pageNumber != prevPageNumber){
AbstractWizardFragment prevFragment = (AbstractWizardFragment) getFragmentAt(prevPageNumber);
prevFragment.onDetachedFromViewPager(wizardActivity.mForm);
}

AbstractWizardFragment currFragment = (AbstractWizardFragment) getFragmentAt(pageNumber);
currFragment.onAttachedToViewPager(wizardActivity.mForm);

prevPageNumber = (short) pageNumber;


Log.d("WizardActivity", "onPageSelected");
}

Although It works fine. There still might be risk, that Fragment won't be found and the method will return null - and then consequently NPE will be thrown.

FragmentPagerAdapter deprecated

UPDATE 2021-06-14: At this point, ViewPager itself is all but deprecated. Technically, ViewPager is not deprecated, but the two concrete PagerAdapter implementations — FragmentPagerAdapter and FragmentStatePagerAdapter — are deprecated. Ideally, switch to something else, such as ViewPager2 or the pager composable in Accompanist.

Replace:

class MyViewPagerAdapter(manager: FragmentManager) : FragmentPagerAdapter(manager)

with:

class MyViewPagerAdapter(manager: FragmentManager) : FragmentPagerAdapter(manager, FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT)

(assuming that MyViewPagerAdapter does not need this value to be configurable)

Unknown number of fragments: FragmentPagerAdapter or FragmentStatePagerAdapter?

Surely go with FragmentStatePagerAdapter.

FragmentPagerAdapter loads all fragments at once and will consume more memory. If you have a lot of fragments, loading all of them at once even may lead to out of memory error.

Even you have known number of fragments, FragmentStatePagerAdapter is recommended in most of the cases.



Related Topics



Leave a reply



Submit