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

Posted by Ixx on Stack Overflow See other posts from Stack Overflow or by Ixx
Published on 2012-07-02T15:12:30Z Indexed on 2012/07/02 15:15 UTC
Read the original article Hit count: 2501

Filed under:
|

I'm getting this on some cases, when going back to the activity, which uses a FragmentStatePagerAdapter, using back button of the devices. 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 = Fragment1.newInstance(param1);
            break;
        case 1:
            fragment = MyFragment2.newInstance(param2, param3);
            break;
    }
    return fragment;
}

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

I also can't debug through the source since the support package doesn't let me attach the source, for some reason... but that's a different question...

© Stack Overflow or respective owner

Related posts about android

Related posts about fragment