0

I have a FragmentActivity with the following xml:

<?xml version="1.0" encoding="utf-8"?>
  <android.support.v4.view.ViewPager
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/pager"
  android:layout_width="match_parent"
  android:layout_height="match_parent" />

and the view pager class is as follows:

public class MyPagerAdapter extends FragmentStatePagerAdapter {

  public MyPagerAdapter(FragmentManager fm) {
      super(fm);
  }

  @Override
  public Fragment getItem(int i) {
    switch (i) {
        case 0:
            // profile fragment activity
            Fragment f = new ProfileFragment();
            return new ProfileFragment();
        case 1:
            // radar fragment activity
            return new RadarFragment();
        case 2:
            // matches fragment activity
            return new MatchesFragment();
    }

    return null;
  }

  @Override
  public int getCount() {
    return 3;
  }
}

I also have a BroadcastReceiver class that has an instance variable of my custom FragmentActivity. Through this instance variable I would like to obtain any of the fragments above. I understand there are FragmentManager.findFragmentById() and findFragmentByTag() methods but I cant seem to find a way of using that in this case.

Wonil
  • 6,364
  • 2
  • 37
  • 55
TomTaila
  • 1,694
  • 1
  • 20
  • 33

1 Answers1

1

Since you only have 3 pages (3 fragments), I recommend that you use the FragmentPagerAdapter rather than the FragmentStatePagerAdapter. Then, you can use findFragmentByTag to find your fragments - read more about that in This SO question.

Community
  • 1
  • 1
Greg Ennis
  • 14,917
  • 2
  • 69
  • 74