Brian
Brian

Reputation: 944

Replacing fragment in ViewPager causes IllegalStateException

Following this example I'm trying to replace one fragement with another within a single tab of a ViewPager. I'm getting this exception: java.lang.IllegalStateException: Can't change tag of fragment DailyWordFragment

class ViewPagerAdapter extends FragmentPagerAdapter {

    private final FragmentManager fragmentManager;
    Fragment fragment;

    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
        fragmentManager = manager;
    }


    @Override
    public Fragment getItem(int position) {

        switch (position){
            case 0:
                if(fragment == null){
                    fragment = DailyWordFragment.newInstance();
                }
                return fragment;
            case 1:
                if(fragment == null){
                    fragment = WordListFragment.newInstance(new WordListFragment.OnWordItemAddListener() {
                        @Override
                        public void onWordItemAddListener() {
                            fragmentManager.beginTransaction().remove(fragment).commit();
                            fragment = AddWordFragment.newInstance();
                            notifyDataSetChanged();
                        }
                    });
                }
               return fragment;
           default:
               return null;
            }
        }

    @Override
    public int getItemPosition(Object object) {
        if(object instanceof WordListFragment && fragment instanceof AddWordFragment)
            return POSITION_NONE;
        return POSITION_UNCHANGED;
    }

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

}

According to other posts this exception seems to be caused by adding the same fragment repeatedly, or getting miscounting the number of fragments but I can't see where either of these are occurring if at all? Clearly I'm missing something. I would appreciate any help. Thank you.

Upvotes: 0

Views: 117

Answers (2)

Juan
Juan

Reputation: 5589

To SWAP fragments in one tab of a View Pager you have to do it in a different way:

You need a fragment that goes in the ViewPager that acts as a container. which is managed by the ViewPager. This Fragment is allways there in the tab, never swaps or changes.

Inside that Fragment you have 2 child fragments that you can swap using transactions with the ChildFragmentManager. But the ViewPager doesn't even know about their existance.

Upvotes: 0

cotnic
cotnic

Reputation: 168

Have you tried before you initialize the Fragment to set it to null?

@Override
public Fragment getItem(int position) {
    fragment = null
    switch (position){
        case 0:
            if(fragment == null){
                fragment = DailyWordFragment.newInstance();
            }
            return fragment;
        case 1:
            if(fragment == null){
                fragment = WordListFragment.newInstance(new WordListFragment.OnWordItemAddListener() {
                    @Override
                    public void onWordItemAddListener() {
                        fragmentManager.beginTransaction().remove(fragment).commit();
                        fragment = AddWordFragment.newInstance();
                        notifyDataSetChanged();
                    }
                });
            }
           return fragment;
       default:
           return null;
        }
    }

Upvotes: 1

Related Questions