Reputation: 4250
I have used FragmentActivity
, ViewPagerAdapter
and Fragment
to build a tab format. There have 3 tabs in my FragmentActivity
. I am confused whether all the Fragment
onCreateView
methods are being called when FragmentActivity
methods are being called or onCreateView methods are called onClick
of the tab? I have set 3 breakpoints onCreateView
methods of all my 3 Fragments
. During debug I found only first 2 onCreateView
methods are being called. Please explain why only 2 are being called?
Upvotes: 0
Views: 466
Reputation: 2781
the ViewPagerAdapter will manage creating fragments and destroying fragments based on the user needs. So if you dont set mViewPager.setOffscreenPageLimit(NUM_TABS-1), the default is 1 (you can refer here)
That why it only create the first 2 tabs, when you select tab3 , then it will destroy the fragment of tab1 and create fragment of tab3. for your case if you set mViewPager.setOffscreenPageLimit(2) when you debug, you will see that it call 3 onCreateView methods and won't destroy view of any fragment. Hope this help.
Upvotes: 1
Reputation: 5696
They are created and destroyed based on the user needs. If you scroll to get to the second fragment you'll see it is created. It's just an optimization. Do you think an application with for example 8 fragments need to keep the 8 fragments in memory ? It would be a waste of resources.
Upvotes: 0