I\'m using a ViewPager
together with a FragmentStatePagerAdapter
to host three different fragments:
You don't need to call getItem()
or some other method at later stage to get the reference of a Fragment
hosted inside ViewPager
. If you want to update some data inside Fragment
then use this approach: Update ViewPager dynamically?
Key is to set new data inside Adaper
and call notifyDataSetChanged()
which in turn will call getItemPosition()
, passing you a reference of your Fragment
and giving you a chance to update it. All other ways require you to keep reference to yourself or some other hack which is not a good solution.
@Override
public int getItemPosition(Object object) {
if (object instanceof UpdateableFragment) {
((UpdateableFragment) object).update(xyzData);
}
//don't return POSITION_NONE, avoid fragment recreation.
return super.getItemPosition(object);
}