3

My FragmentActivity contains ViewPager with some pages. Once one page is modified, others need to receive notification. So my steps: 1. Send notification from fragment to activity 2. Getting list of fragments 3. Call notify to each one.

The problem is in step 2, to get fragments. Calling adapter.getItem(int i) calls MyFragmentAdapter.getItem(int i), which returns new Fragment, that is not attached to activity.

Any ideas?

A-Sharabiani
  • 17,750
  • 17
  • 113
  • 128
kzotin
  • 5,365
  • 3
  • 29
  • 36

2 Answers2

3

Adding next methods to YourFragmentPagerAdapter solves the problem:

public Fragment getActiveFragment(ViewPager container, int position) {
    String name = makeFragmentName(container.getId(), position);
    return  mFragmentManager.findFragmentByTag(name);
}

private static String makeFragmentName(int viewId, int index) {
    return "android:switcher:" + viewId + ":" + index;
}
kzotin
  • 5,365
  • 3
  • 29
  • 36
  • This solution will work as long as the internal static method `FragmentPagerAdapter.makeFragmentName(int viewID, int index)` keeps using the same string to save the created fragments. See `FragmentPagerAdapter.instantiateItem` for details. Perhaps you should just use – Diederik Feb 24 '12 at 11:59
  • erm, as I was saying: On the off chance that you are using JakeWharton's ActionBarSherlock, he has modified the support library so that this method is public. – Diederik Feb 24 '12 at 12:08
0

Why not just send out a broadcast from the Fragment that has changed and then have a broadcast receiver in your other fragments that receive the broadcast?

Kurtis Nusbaum
  • 30,445
  • 13
  • 78
  • 102
  • Yeah that's a solution. But obviously you should have an ability just to iterate all fragments in ViewPager. – kzotin Nov 03 '11 at 18:21