0

I'm not able to get rid of this error, no matter how many solutions I applied.

************ CAUSE OF ERROR ************ java.lang.IllegalStateException: Fragment b{4210f838} is not currently in the FragmentManager at android.support.v4.app.i.a(FragmentManager.java:603) at android.support.v4.app.j.a(FragmentStatePagerAdapter.java:136) at android.support.v4.view.ViewPager.b(ViewPager.java:874) at android.support.v4.view.ViewPager$h.onChanged(ViewPager.java:2824) at android.database.DataSetObservable.notifyChanged(DataSetObservable.java:37) at android.support.v4.view.k.c(PagerAdapter.java:276) at com.xyz.Activity2$2.a(Activity2.java:351) at com.a.a.d$1.onAnimationEnd(ParallelAnimator.java:73) at android.animation.AnimatorSet$AnimatorSetListener.onAnimationEnd(AnimatorSet.java:765) at android.animation.AnimatorSet$AnimatorSetListener.onAnimationEnd(AnimatorSet.java:765) at android.animation.ValueAnimator.endAnimation(ValueAnimator.java:1018) at android.animation.ValueAnimator.access$400(ValueAnimator.java:51) at android.animation.ValueAnimator$AnimationHandler.doAnimationFrame(ValueAnimator.java:623) at android.animation.ValueAnimator$AnimationHandler.run(ValueAnimator.java:639) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725) at android.view.Choreographer.doCallbacks(Choreographer.java:555) at android.view.Choreographer.doFrame(Choreographer.java:524) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711) at android.os.Handler.handleCallback(Handler.java:615) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:4898) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773) at dalvik.system.NativeStart.main(Native Method)

Code I'm using:

public class PlaceSlidesFragmentAdapter extends FragmentStatePagerAdapter implements IconPagerAdapter {

private List<String> imgList;

Activity activity;

public PlaceSlidesFragmentAdapter(Activity act, FragmentManager fm) {
    super(fm);

    imgList = new ArrayList<String>();
    activity= act;
}

public void addImage(String imgUrl)
{
    imgList.add(imgUrl);
}

public void addAllImages()
{
    for(All Image URLs)
    {
        addImage(s);
    }
}

public void removeAllImages()
{
    imgList.clear();
}

public String getImageName(int pos)
{
    return imgList.get(pos);
}


@Override
public Fragment getItem(int position) {
    return new PlaceSlideFragment(imgList.get(position));
}

@Override
public int getCount() {
    return imgList.size();
}

@Override
public int getIconResId(int index) {
    return 0;
}

@Override
public int getItemPosition(Object object){
    return PagerAdapter.POSITION_NONE;
}

}

I'm getting error on calling onNotifyDataSetChanged(). Basically I've this viewpager in an activity and I add/remove fragments dynamically on runtime.Using this:

mAdapter.removeAllImages();
mAdapter.addAllImages();        
mAdapter.notifyDataSetChanged();

The crash occurs over 20-times per day with ~500-700 users/day. I'm not able to reproduce it on my system yet. Let me know if you need any other information.

If anybody could help through this. It would be a great help. Thanks

Edit:

        // set-up image view-pager
    mAdapter = new PlaceSlidesFragmentAdapter(this, getSupportFragmentManager());
    mPager = (ViewPager)findViewById(R.id.pager);
    mPager.setAdapter(mAdapter);
shadow
  • 90
  • 11
  • Try initializing the `ViewPager` with `getChildFragmentManager()` rather than `getFragmentManager()`. Let me know if that works. – Sebastiano Mar 17 '15 at 10:27
  • getChildFragmentManager() is a method on Fragment, not on FragmentActivity. I'm initialising viewpager under fragmentActivity. I've add code of initialization in description. – shadow Mar 19 '15 at 09:56
  • Could the problem be related to activity restoration? Try enabling the developer option to "Destroy activities" to reproduce that. Also, you should not use a constructor to instantiate your fragments, but a static creator, like done in this example http://developer.android.com/reference/android/support/v4/app/FragmentStatePagerAdapter.html – Gyome Mar 19 '15 at 22:44
  • This might help >> [FragmentStatePagerAdapter][1] [1]: http://stackoverflow.com/questions/11296411/fragmentstatepageradapter-illegalstateexception-myfragment-is-not-currently – ch3tanz Mar 22 '15 at 17:52
  • Hi can you post the code from where you would called the notifyDataSetChanged() method. I suppose to assume that you were calling it from after animation end. just post the code from where you called it. – Bhavdip Sagar Mar 23 '15 at 06:02
  • I think it was not related to method.The **getFragmentManager()** is correct way you used. – Bhavdip Sagar Mar 23 '15 at 06:04
  • When are you calling `mAdapter.removeAllImages(); mAdapter.addAllImages(); mAdapter.notifyDataSetChanged();`? It's important to know if it's after an asynchronous thread being done or inside `onResume()` or any other occassion for your adapter not being in sync with the FragmentManager. Your error clearly occurs because the system sees an old Fragment still being used by your adapter, that should have already been cleared because of a configuration change or waking up state or anything that causes your adapter to change. – einschnaehkeee Mar 23 '15 at 23:50

2 Answers2

0

When creating/commiting the fragment use

getChildFragmentManager() 

instead of

getFragmentManager()

This should work since the adapter hierarchy is the fragment is a child of the tab-manager.

EDIT:

try this:

mAdapter = new PlaceSlidesFragmentAdapter(this, getChildFragmentManager());
mPager = (ViewPager)findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
Joakim Engstrom
  • 6,243
  • 12
  • 48
  • 67
0

As already stated by my comment, your adapter is clearly out of sync with the FragmentManager. You're doing dynamic stuff with your pageradapter and the only safe way of doing dynamic stuff within a pageradapter is by this method: Retrieve a Fragment from a ViewPager

If you implement it like that, you shouldn't get any illegal state inside your adapter.

Community
  • 1
  • 1
einschnaehkeee
  • 1,858
  • 2
  • 17
  • 20