+3

Difference between FragmentPagerAdapter and FragmentStatePagerAdapter, getSupportFragmentManager() and getChildFragmentManager()

Difference between FragmentPagerAdapter and FragmentStatePagerAdapter

Here is a log lifecycle of each fragment in ViewPager which have 4 fragment and offscreenPageLimit = 1 (default value)

FragmentStatePagerAdapter

Go to Fragment1 (launch activity)

Fragment1: onCreateView
Fragment1: onStart
Fragment2: onCreateView
Fragment2: onStart

Go to Fragment2

Fragment3: onCreateView
Fragment3: onStart

Go to Fragment3

Fragment1: onStop
Fragment1: onDestroyView
Fragment1: onDestroy
Fragment4: onCreateView
Fragment4: onStart

Go to Fragment4

Fragment2: onStop
Fragment2: onDestroyView
Fragment2: onDestroy

FragmentPagerAdapter

Go to Fragment1 (launch activity)

Fragment1: onCreateView
Fragment1: onStart
Fragment2: onCreateView
Fragment2: onStart

Go to Fragment2

Fragment3: onCreateView
Fragment3: onStart

Go to Fragment3

Fragment1: onStop
Fragment1: onDestroyView
Fragment4: onCreateView
Fragment4: onStart

Go to Fragment4

Fragment2: onStop
Fragment2: onDestroyView

Conclusion: FragmentStatePagerAdapter call onDestroy when the Fragment is overcome offscreenPageLimit while FragmentPagerAdapter not.

Note: I think we should use FragmentStatePagerAdapter for a ViewPager which have a lot of page because it will good for performance.

Example of offscreenPageLimit:

If we go to Fragment3, it will detroy Fragment1 (or Fragment5 if have) because offscreenPageLimit = 1. If we set offscreenPageLimit > 1 it will not destroy.
If in this example, we set offscreenPageLimit=4, there is no different between using FragmentStatePagerAdapter or FragmentPagerAdapter because Fragment never call onDestroyView and onDestroy when we change tab

Difference between getSupportFragmentManager() and getChildFragmentManager()?

getFragmentManager belong to Activity
getChildFragmentManager belong to Fragment

Example we have a app which have MainActivity, Fragment1, Fragment2, container_view_on_main is a layout in activty_main.xml

TO display Fragment1 on MainActivity we must use getSupportFragmentManager() getSupportFragmentManager().beginTransaction().replace(R.id.container_view_on_main, Fragment1.newInstance());

TO display Fragment2 from Fragment1 we have 2 way USE getFragmentManager() getFragmentManager().beginTransaction().replace(R.id.container_view_on_main, Fragment1.newInstance()); USE getChildFragmentManager() First, we have to create a layout with id container_view_on_fragment1 inside fragment1.xml, then getChildFragmentManager().beginTransaction().replace(R.id.container_view_on_fragment1, Fragment2.newInstance()).commit();

CONCLUSION In this demo, I think we should use getFragmentManager() when go from Fragment1 to Fragment2 because it is simple and good for performance (Fragment1 will stop when Fragment2 open) When we use getChildFragmentManager()?
Example your MainActivity have a ViewPager which have 3 pages, inside each pages you need to replace some fragment.


All rights reserved

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí