4

I have following issue. I have ViewPager and FragmentPagerAdapter for it.

All I need to do is call some method from my Fragment which is in ViewPager when this page was selected.

I tried to keep all my Fragments in List inside my adapter, but the problem is that when I rotate my device Adapter use previous fragments. New fragments were created after rotation and I have list of them, but I don't now how to get access to previous fragments. I have references to new fragments only.

Here is my adapter:

public class MainPagerAdapter extends FragmentPagerAdapter {
    private List<Fragment> fragmentList;

    public MainPagerAdapter(FragmentManager fm) {
        super(fm);
        fragmentList = new ArrayList<Fragment>();
        fragmentList.add(new TaskPageFragment());
        fragmentList.add(new HistoryFragment());
        fragmentList.add(new TestFragment());
    }

    @Override
    public Fragment getItem(int i) {
        return fragmentList.get(i);
    }

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

Of course when I trying to access to fragment which was only created like an object, I can't get access to Activity Context.

Fragment fragment = pagerAdapter.getItem(i);
if (fragment instanceof SelectionListener) {
     ((SelectionListener)fragment).onTabSelected();
}

Here is onTabSelected() method call is shown. It's my interface which I implemented to each fragment in ViewPager and when it gets called after screen rotation I get null Context

Dmitriy
  • 830
  • 3
  • 15
  • 29

1 Answers1

0

Device configurations can change during runtime (such as screen orientation). When this happens the Activity is restarted. This is done so your activities can adjust to match the new device configuration, and since fragments are within Activities they are also 'destroyed'.

However your issue may be solved with something simple, since from what I understand from your question you are not looking for UI configuration changes and assuming you are using API 11+.

public void setRetainInstance (boolean retain)

Since: API Level 11

Control whether a fragment instance is retained across Activity re-creation (such as from a configuration change). This can only be used with fragments not in the back stack. If set, the fragment lifecycle will be slightly different when an activity is recreated:

  • onDestroy() will not be called (but onDetach() still will be, because the fragment is being detached from its current activity).
  • onCreate(Bundle) will not be called since the fragment is not being re-created.
  • onAttach(Activity) and onActivityCreated(Bundle) will still be called.
public MainPagerAdapter(FragmentManager fm) {
    Fragment frag = null;
    super(fm);
    fragmentList = new ArrayList<Fragment>();
    frag = new TaskPageFragment();
    frag.setRetainInstance(true);
    fragmentList.add(frag);

    frag = new HistoryFragment();
    frag.setRetainInstance(true);
    fragmentList.add(frag);

    frag = new TestFragment();
    frag.setRetainInstance(true);
    fragmentList.add(frag);
}
docslax
  • 139
  • 1
  • 5
  • it won't help. the problem is FragmentPagerAdapter sets ID to each fragment when I create it. after rotation adapter will check if there are any fragments with same ID. if so, it will return reference to old fragment. so setRetainInstance won't help me at all – Dmitriy Jun 30 '12 at 03:53
  • My mistake I thought your issue was that on screen orientation change you were getting new fragments created with a missing Activity Context and that you wanted to keep the old instance. Unfortunately now that I re-read your problem I'm not sure I understand at all what your issue is... sorry :( – docslax Jul 05 '12 at 18:33