0

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.

Brian
  • 924
  • 13
  • 22
  • Why are you doing things with the fragment manager inside the viewpager? In getitem you just should return an instance of the Fragment. ViewPager manages the lifecycle of the fragment instances. – Juan Jul 27 '17 at 12:45
  • Because I'm trying to do a fragment swap within one tab. – Brian Jul 27 '17 at 12:47

2 Answers2

1

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;
        }
    }
cotnic
  • 158
  • 2
  • 11
  • I'm pretty sure null is the default value of an object in java. Thanks though. – Brian Jul 27 '17 at 12:57
  • Yes but if you have it initialized from before it dosen't go back to null. So in case you are on a fragment and still in the same activity the fragment dosen't go to null but is still initialized when you click for the other fragment. – cotnic Jul 27 '17 at 13:05
  • Ah, you are of course correct. But I went with the other answer and marked your answer as useful. – Brian Jul 31 '17 at 14:16
0

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.

Juan
  • 5,525
  • 2
  • 15
  • 26
  • Thanks I'll give your suggestion a go. I'll come back and mark this correct at a later time if it works. – Brian Jul 27 '17 at 12:58