Why is this "IllegalStateException: Fragment already added” happen

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 1165 Views - Last Post: 10 May 2019 - 02:05 AM

#1 genericname   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 09-May 19

Why is this "IllegalStateException: Fragment already added” happen

Posted 09 May 2019 - 07:59 PM

I'm getting an error : IllegalStateException: Fragment already added

Any idea how to prevent this? I need to return "myFragment", so simply checking if isAdded() wont solve it. Pleas let me know if any further info is needed. Thank you immensely. I've been stuck on this for days... The following is in my fragment adapter:

@Override
 public Fragment getItem(int position) {
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        MyFragment myFragment = new MyFragment();
        fragmentTransaction.add(R.id.myViewPager, myFragment, "myFrag");
        fragmentTransaction.commit();

        Bundle bundle = new Bundle();
        bundle.putInt("position", position);
        myFragment.setArguments(bundle);

       return myFragment;
    }



Is This A Good Question/Topic? 0
  • +

Replies To: Why is this "IllegalStateException: Fragment already added” happen

#2 wseng92   User is offline

  • D.I.C Addict
  • member icon

Reputation: 55
  • View blog
  • Posts: 707
  • Joined: 23-September 15

Re: Why is this "IllegalStateException: Fragment already added” happen

Posted 09 May 2019 - 08:03 PM

Maybe remove the old fragment?
Was This Post Helpful? 0
  • +
  • -

#3 genericname   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 09-May 19

Re: Why is this "IllegalStateException: Fragment already added” happen

Posted 09 May 2019 - 08:10 PM

View Postwseng92, on 09 May 2019 - 08:03 PM, said:

Maybe remove the old fragment?


Thanks for the suggestion. I tried that. The replacement fragment is empty when I remove/replace it.
Was This Post Helpful? 0
  • +
  • -

#4 wseng92   User is offline

  • D.I.C Addict
  • member icon

Reputation: 55
  • View blog
  • Posts: 707
  • Joined: 23-September 15

Re: Why is this "IllegalStateException: Fragment already added” happen

Posted 09 May 2019 - 08:13 PM

Are you trying to create FragmentPagerAdapter? Can you show us how you create?
Was This Post Helpful? 0
  • +
  • -

#5 genericname   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 09-May 19

Re: Why is this "IllegalStateException: Fragment already added” happen

Posted 09 May 2019 - 08:22 PM

View Postwseng92, on 09 May 2019 - 08:13 PM, said:

Are you trying to create FragmentPagerAdapter? Can you show us how you create?


FragmentStatePagerAdapter.

In MainActivity:

CalFragmentAdapter adapter = new CalFragmentAdapter(getChildFragmentManager());



I've also tried getFragmentManager().
Was This Post Helpful? 0
  • +
  • -

#6 wseng92   User is offline

  • D.I.C Addict
  • member icon

Reputation: 55
  • View blog
  • Posts: 707
  • Joined: 23-September 15

Re: Why is this "IllegalStateException: Fragment already added” happen

Posted 09 May 2019 - 08:31 PM

Post more code please. Maybe create a small compiling and running projects?
Was This Post Helpful? 0
  • +
  • -

#7 genericname   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 09-May 19

Re: Why is this "IllegalStateException: Fragment already added” happen

Posted 09 May 2019 - 09:35 PM

View Postwseng92, on 09 May 2019 - 08:31 PM, said:

Post more code please. Maybe create a small compiling and running projects?


I'm working on it. I have a lot of code.
Was This Post Helpful? 0
  • +
  • -

#8 genericname   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 09-May 19

Re: Why is this "IllegalStateException: Fragment already added” happen

Posted 09 May 2019 - 09:56 PM

View Postwseng92, on 09 May 2019 - 08:31 PM, said:

Post more code please. Maybe create a small compiling and running projects?


This is everything I have:

- CalendarHome (Fragment)
- CalFragment (Fragment) only has a textView in it to test if it's working.
- CalFragment's adapter is CalFragmentAdapter.

CalendarHome:
public class CalendarHome extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.calendar_home, container, false);

        ViewPager vp = (ViewPager) view.findViewById(R.id.pager);

        CalFragmentAdapter adapter = new CalFragmentAdapter(getChildFragmentManager());
        vp.setAdapter(adapter);

        return view;
    }
}




CalFragmentAdapter:
public class CalFragmentAdapter extends FragmentStatePagerAdapter {

    private FragmentManager fragmentManager;

    public CalFragmentAdapter(FragmentManager fm) {
        super(fm);
        this.fragmentManager = fm;
    }

    @Override
    public Fragment getItem(int position) {

        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        CalFragment calFragment = new CalFragment();
        fragmentTransaction.add(R.id.pager, calFragment, "calFrag");
        fragmentTransaction.commit();

        return calFragment;
    }

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


Was This Post Helpful? 0
  • +
  • -

#9 wseng92   User is offline

  • D.I.C Addict
  • member icon

Reputation: 55
  • View blog
  • Posts: 707
  • Joined: 23-September 15

Re: Why is this "IllegalStateException: Fragment already added” happen

Posted 10 May 2019 - 12:08 AM

Why you need to add fragment in getItem method again ?

Refer this https://www.truiton....dapter-example/
Was This Post Helpful? 0
  • +
  • -

#10 ben255   User is offline

  • D.I.C Addict

Reputation: 44
  • View blog
  • Posts: 528
  • Joined: 09-September 13

Re: Why is this "IllegalStateException: Fragment already added” happen

Posted 10 May 2019 - 12:28 AM

your committing then returning the fragment, seems odd. are you trying to do a tab layout?
Was This Post Helpful? 0
  • +
  • -

#11 genericname   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 09-May 19

Re: Why is this "IllegalStateException: Fragment already added” happen

Posted 10 May 2019 - 12:58 AM

View Postben255, on 10 May 2019 - 12:28 AM, said:

your committing then returning the fragment, seems odd. are you trying to do a tab layout?


I need to set a tag for the fragment. That's the only reason for the fragmentTransaction.commit(). Is there a way around it?
Was This Post Helpful? 0
  • +
  • -

#12 ben255   User is offline

  • D.I.C Addict

Reputation: 44
  • View blog
  • Posts: 528
  • Joined: 09-September 13

Re: Why is this "IllegalStateException: Fragment already added” happen

Posted 10 May 2019 - 01:04 AM

    public Fragment getItem(int i) {
        Fragment fragment = new SomeFrag();
        Bundle args = new Bundle();
        // Our object is just an integer :-P
        args.putInt(SomeFrag.ARG_OBJECT, i + 1);
        fragment.setArguments(args);
        return fragment;
    }



the return statement will start a fragment when you commit you start another one thats probably why get the error. also change get count to 4 or you will start 200 tabs

This post has been edited by ben255: 10 May 2019 - 01:54 AM

Was This Post Helpful? 0
  • +
  • -

#13 genericname   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 09-May 19

Re: Why is this "IllegalStateException: Fragment already added” happen

Posted 10 May 2019 - 01:29 AM

Would you mind explaining this line?

args.putInt(GraphFragment.ARG_OBJECT, i + 1);

Was This Post Helpful? 0
  • +
  • -

#14 ben255   User is offline

  • D.I.C Addict

Reputation: 44
  • View blog
  • Posts: 528
  • Joined: 09-September 13

Re: Why is this "IllegalStateException: Fragment already added” happen

Posted 10 May 2019 - 01:35 AM

its the first argument is a Key and the second is just the value in this case its the tab position that the fragment is in so if you search the bundle in the fragment it will know what tab you're in
Was This Post Helpful? 1
  • +
  • -

#15 genericname   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 09-May 19

Re: Why is this "IllegalStateException: Fragment already added” happen

Posted 10 May 2019 - 01:53 AM

View Postben255, on 10 May 2019 - 01:35 AM, said:

its the first argument is a Key and the second is just the value in this case its the tab position that the fragment is in so if you search the bundle in the fragment it will know what tab you're in


The problem here is that I need to access the fragment instance from an external class. (This is for liveData)
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2