When is FragmentPagerAdapter's getItem called?
Asked Answered
S

3

41

I'm writing an application that uses the FragmentPagerAdapter. The fragments in the adapter need to be updated according to outside data - but that doesn't happen. I noticed that the fragment classes are only instantiated once, in the getItem function:

@Override
public Fragment getItem(int position) {
    TabInfo info = mTabs.get(position);
    return Fragment.instantiate(mContext, info.clss.getName(),
                info.args);
}

Even if I delete the class and use a new one, nothing helps - this method is only called once, the first time that the tab is populated, and then never again. Anyone has an idea why?

Thanks!

Slater answered 12/10, 2013 at 21:43 Comment(0)
L
53

getItem will be called whenever the adapter needs a fragment and the fragment does not exist.

If the fragment already exists in the FragmentManager then there is no need to instantiate it and getItem does not need to be called.

To update an existing fragment you would need to retrieve it from the FragmentManager or the adapter and manipulate it accordingly.

By default, the viewpager will create fragments for the visible page and the one next to it. I.e to start with, fragments in position 1 and 2. When you swipe to page 2, the fragment at position 3 will be created etc

Layla answered 12/10, 2013 at 21:46 Comment(9)
is it possible to destroy a fragment?Slater
#10396821Layla
#15212809Layla
@n00bprogrammer can you please tell me how you can resolve this problem because i also get the same problem.Demodulation
+1 for the info about ViewPager creating fragments for the visible page and the one next to it. I wondered why I kept seeing things in tab 1 firing when only tab 0 was visible.Ushijima
In public Fragment getItem(int position) is position 0 based? @LaylaPome
I think so but not looked at it for a while so could be wrongLayla
"If the fragment already exists in the FragmentManager" how exactly is this determined? ie. how does it know exactly what it's looking for?Alwyn
From the Docs "ViewPager associates each page with a key Object instead of working with Views directly. This key is used to track and uniquely identify a given page independent of its position in the adapter." developer.android.com/reference/android/support/v4/view/…Layla
T
19

To be more specific than the answer above (which is correct!), getItem is called by FragmentPagerAdapter's instantiateItem(ViewGroup container, int position) method. Just in case that helps :)

Tragedy answered 12/3, 2014 at 20:20 Comment(0)
F
4

Simply use FragmentStatePagerAdapter instead of FragmentPagerAdapter

FragmentStatePagerAdapter destroys the instance of unneeded instance of fragments and instantiates again on-demand basis. On the other hand, FragmentPagerAdapter just detach the Fragment and reattach it. So fragments created in FragmentPagerAdapter never get destroyed. That's why I always prefer FragmentStatePagerAdapter.

Ferric answered 20/6, 2016 at 13:33 Comment(1)
I know this is very old but ill just add in anyway incase. Fragment pager adapter holds on to the fragments in memory, used if there are only a couple of tabs. Fragment state pager adapter will destroy the fragment instance on every swipe to save on memoryWoebegone

© 2022 - 2024 — McMap. All rights reserved.