Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FragmentStatePagerAdapter IllegalStateException: <MyFragment> is not currently in the FragmentManager

I'm getting this on some cases, in onResume(), of an activity which uses a FragmentStatePagerAdapter. When using device's back button. Not always. Not reproducible.

I'm using support package v4, last revision (8).

Already searched with google, no success finding a useful answer.

Looking in the source, it's thrown here: FragmentManager.java

@Override
public void putFragment(Bundle bundle, String key, Fragment fragment) {
    if (fragment.mIndex < 0) {
        throw new IllegalStateException("Fragment " + fragment
                + " is not currently in the FragmentManager");
    }
    bundle.putInt(key, fragment.mIndex);
}

But why is the index of fragment < 0 there?

The code instantiating the fragments:

@Override
public Fragment getItem(int position) {
    Fragment fragment = null;

    switch(position) {
        case 0:
            fragment = MyFragment.newInstance(param1);
            break;
        case 1:
            fragment = MyFragment2.newInstance(param2, param3);
            break;
    }
    return fragment;
}

@Override
public int getCount() {
    return 2;
}
like image 585
User Avatar asked Jul 02 '12 15:07

User


4 Answers

If your ViewPager is layouted inside a fragment (not an activty) :

mViewPager.setAdapter(new MyFragmentStatePagerAdapter(getChildFragmentManager()));

like image 197
Thomas G. Avatar answered Nov 09 '22 09:11

Thomas G.


I had the same error here, but for another reason.

In my case I had override getItemPosition in my FragmentStatePagerAdapter. My ideia was to return the position, if the item exists, or a POSITION_NONE, if it doesn't exists anymore.

Well, my problem was the fact that when my collection got empty I returned POSITION_NONE. And that broke everything.

My fix was to return POSITION_UNCHANGED when I had an empty collection.

Hope it helps someone else.

like image 20
laggedHero Avatar answered Nov 09 '22 09:11

laggedHero


The two key things to understand the bug are:

  1. It happens sometimes.
  2. It happens in onResume().

Given this information, it's likely that the ViewPager is not retaining the state of your Fragments. If you are manipulating the Fragments directly from the Activity, it could be the case that the off-page Fragment is getting destroyed and your Activity is trying to manipulate a null fragment. To retain the Fragment's state even when it is not in the current screen, the fix is pretty simple:

private static final int NUM_ITEMS = 2;

ViewPager mPager = /** instantiate viewpager **/;
mPager.setOffscreenPageLimit(NUM_ITEMS-1);

You can read about it here:

ViewPager Fragments getting destroyed over time?

like image 8
Alex Lockwood Avatar answered Nov 09 '22 09:11

Alex Lockwood


Got it, the reason was, that I'm intantiating the Adapter each time in onResume().

If I instantiate the adapter only once, in the life cycle of the activity, this does not happen anymore.

like image 5
User Avatar answered Nov 09 '22 07:11

User