Reputation: 1499
I would like to load data from network into fragments. Inside the Activity
I have a ViewPager
and I am using a FragmentPagerAdapter
to provide fragments to it. The problem is:
How can I detect from activity, that ALL fragments from ViewPager are created and ready to show data. How can I inform fragments, that there is a data inside activity, that should be shown?
Upvotes: 0
Views: 170
Reputation: 87064
How can I detect from activity, that ALL fragments from ViewPager are created and ready to show data.
This sounds a bit strange to ask. You Activity
doesn't need to know when all of the ViewPager
fragments are initialized(as only one will actually be seen by the user(plus one on each side will be available if you don't mess with setOffScreenPageLimit()
), it doesn't make sense to update the rest).
How can I inform fragments, that there is a data inside activity, that should be shown?
You fragments could register themselves as listeners for an Activity
data load event. The Activity
will do it's job of getting the data and then call the update method on all of the registered fragments(which should be stored in WeakReferences
to avoid holding on to them when we shouldn't). The main problem is you'll risk trying to update items
Another hacky approach is to use something like from the Activity
:
// get the current fragment(add/subtract one for the available fragments on each side of the curently visible
Fragment f = getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.ViewPagerId + ":" + mViewPager.getCurrentItem()); one.)
f.update();
The rest of the fragments will check for new data being available when they get recreated.
Upvotes: 1