FragmentStatePagerAdapter with dynamic data, IllegalStateException

290 views Asked by At

I have a main fragment that has list of items, on clicking an item, I open detailed view of item in a new fragment

using

transaction.replace(R.id.container, FeedDetail.newInstance(title, position)).commit();

The detailed fragment has FragmentStatePagerAdapter to show sliding list of items. Everything works right, but the main list of item gets updated in the background (AsyncTask). When sliding items in detailed view, I get

java.lang.IllegalStateException: The application's PagerAdapter changed the adapter's contents without calling PagerAdapter#notifyDataSetChanged! Expected adapter item count: 30, found: 40 Pager id

I properly call adapter.notifyDataSetChanged() from onPostExecute(), still could not fix this issue. I guess this issue could be similar to the one shared here http://translate.google.co.in/translate?hl=en&sl=zh-CN&u=http://blog.csdn.net/hlglinglong/article/details/34845519&prev=search but not sure though. I have tried with different values in getItemPosition() of adapter without success. It would be nice to get some hint on this issue. I am pasting the detailed fragment code here for reference,

public class FeedDetail extends Fragment {
    private ViewPager mPager = null;
    private PagerAdapter mPagerAdapter;
    private OnFragmentInteractionListener mListener;
    private FragmentActivity myContext;
    public static String title;
    public  static Integer pos;
    private LayoutInflater mInflater;
    private View  mRootView = null;
    ViewGroup mViewGroup = null;
    public static FeedDetail newInstance(String title,  int pos) {
        FeedDetail fragment = new FeedDetail();
        Bundle args = new Bundle();
        args.putString("title", title);
        args.putInt("pos", pos);
        fragment.setArguments(args);
        return fragment;
    }
    public FeedDetail() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            title = getArguments().getString("title");            
            pos = getArguments().getInt("pos");
        }
    }


    @Override
    public void onDestroyView()
    {
        super.onDestroyView();
        ViewGroup parentViewGroup = (ViewGroup) mRootView.getParent();
        if( null != parentViewGroup ) {
            parentViewGroup.removeAllViews();
        }
        mViewGroup.removeAllViews();
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        mViewGroup = container;
        if (mRootView == null) {
            mRootView = (View) inflater.inflate(R.layout.feedpager, null, false);
        }
        else {
            ViewGroup parent = (ViewGroup) mRootView.getParent();
            parent.removeView(mRootView);
        }
        mPager = (ViewPager) mRootView.findViewById(R.id.pager);
        mPagerAdapter = new ScreenSlidePagerAdapter(getChildFragmentManager());
        mPager.setAdapter(mPagerAdapter);
        mPager.setCurrentItem(pos);
        mRootView.setFocusableInTouchMode(true);
        mRootView.requestFocus();
        return  mRootView;
    }




    @Override
    public void onAttach(Activity activity) {
        myContext=(FragmentActivity) activity;
        super.onAttach(activity);
    }


    @Override
    public void onDetach() {
        super.onDetach();

        try {
            Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");
            childFragmentManager.setAccessible(true);
            childFragmentManager.set(this, null);

        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
    public static interface OnFragmentInteractionListener {
        public void onFragmentInteraction(Uri uri);
    }

    private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter{
        public ScreenSlidePagerAdapter(FragmentManager fm) {
            super(fm);
            setRetainInstance(true);
        }

        @Override
        public Fragment getItem(int position) {
            return ScreenSlidePageFragment.newInstance(position);
        }

        @Override
        public int getItemPosition(Object object) {
            return 1;
        }

        @Override
        public int getCount() {
            return Navigation.adapter.getCount();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return Navigation.adapter.getFeeds().get(position).title;

        }
    }
}
0

There are 0 answers