45

My ERROR :

java.lang.IllegalStateException: commit already called

My CODE:

final FragmentTransaction fragmentTransaction =getFragmentManager().beginTransaction();

f1_fragment  = new F1_Fragments();
f2_fragment = new F2_Fragments();

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            parent.getItemAtPosition(position);

            if(position==0){
                fragmentTransaction.replace(android.R.id.content, f1_fragment);
            }else{
                fragmentTransaction.replace(android.R.id.content, f2_fragment);
            }

            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
        }
    });
Bryan Herbst
  • 62,910
  • 10
  • 119
  • 113
JPs
  • 461
  • 1
  • 4
  • 6
  • 7
    you already made a commit on that transaction, you need to start a new transaction if you want to make a commit again `fragmentTransaction = getFragmentManager().beginTransaction();` – tyczj Jul 03 '14 at 19:30

7 Answers7

67

You are beginning the FragmentTransaction outside of the OnItemClickListener. Thus you are attempting to commit() a single FragmentTransaction every time the user clicks an item in your ListView.

You need to begin a new FragmentTransaction every time you intend to perform any number of Fragment operations.

A simple fix would look like this:

f1_fragment  = new F1_Fragments();
f2_fragment = new F2_Fragments();

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        FragmentTransaction fragmentTransaction =getFragmentManager().beginTransaction();

        parent.getItemAtPosition(position);

        if(position==0){
            fragmentTransaction.replace(android.R.id.content, f1_fragment);
        }else{
            fragmentTransaction.replace(android.R.id.content, f2_fragment);
        }

        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();
    }
});
Bryan Herbst
  • 62,910
  • 10
  • 119
  • 113
10

you can use this method for replace fragment with each other just call this

do those are global

YourFragment1 frg1 = new   YourFragment1 ();    
YourFragment2 frg1 = new   YourFragment2 (); 

And then call it by

openFragment(frg1); 

or

  openFragment(frg2);

OpenFragment:

  private void openFragment(final Fragment fragment)   {
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction transaction = fragmentManager.beginTransaction();        
    transaction.replace(R.id.container, fragment);
    transaction.addToBackStack(null);
    transaction.commit();

}
insomniac
  • 9,378
  • 6
  • 39
  • 52
idris yıldız
  • 2,039
  • 17
  • 20
6

Look at this scenario

I need to add new fragment on every list item click. Just initialise fragment transaction on every item click.

switch (v.getId()) {

                case R.id.tvaddExpense:

                    fragmentTransaction = getFragmentManager().beginTransaction();
                    fragmentTransaction.add(R.id.containerFucntionsList, new Fragment1());
                    fragmentTransaction.addToBackStack(null);
                    fragmentTransaction.commit();
                    break;

                case R.id.relEvents:
                    fragmentTransaction = getFragmentManager().beginTransaction();
                    fragmentTransaction.replace(R.id.containerFucntionsList, new Fragment2());
                    fragmentTransaction.addToBackStack(null);
                    fragmentTransaction.commit();
                    break;
            } 
Punithapriya
  • 59
  • 1
  • 5
5

The Error

java.lang.IllegalStateException: commit already called

shows that the FragmentTransaction has been completed after calling commit() the first time and you are again calling commit() which tends to complete it once again. Hence it makes an Illegal state for the FragmentTransaction.

As per your code, you are using the same FragmentTransaction for changing fragments. However, after the first commit() call, the FragmentTransaction has completed and you need to begin it again to perform any operation on Fragments.

You can change your ClickListner as:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override

    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            fragmentTransaction = getSupportFragmentManager().beginTransaction();   

            parent.getItemAtPosition(position);

            if(position==0){
                fragmentTransaction.replace(android.R.id.content, f1_fragment);
            }else{
                fragmentTransaction.replace(android.R.id.content, f2_fragment);
            }

            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
        }
    });

I hope it clears your doubt.

Michael
  • 1,393
  • 3
  • 18
  • 26
4

I had same issue and I have solved by creating new instance of FragmentTransaction.

Just add everytime below line before add / replace fragment.

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

Hope this would help you.

Hiren Patel
  • 48,538
  • 20
  • 161
  • 144
2

You must define a FragmentTransAction object separately each time you add a fragment:

     switch (position){
                case 0:
                    FragmentTransaction fragmentTransaction1 = getSupportFragmentManager().beginTransaction();
                    fragmentTransaction1.replace(R.id.parentOfMenu, new FragmentHome());
                    fragmentTransaction1.commit();

                    break;
                case 1:
                    FragmentTransaction fragmentTransaction2 = getSupportFragmentManager().beginTransaction();
                    fragmentTransaction2.replace(R.id.parentOfMenu, new FragmentMedia());
                    fragmentTransaction2.commit();

                    break;

                case 2:
                    FragmentTransaction fragmentTransaction3 = getSupportFragmentManager().beginTransaction();
                    fragmentTransaction3.replace(R.id.parentOfMenu, new FragmentChart());
                    fragmentTransaction3.commit();

                    break;

                case 3:
                    FragmentTransaction fragmentTransaction4 = getSupportFragmentManager().beginTransaction();
                    fragmentTransaction4.replace(R.id.parentOfMenu, new FragmentProfile());
                    fragmentTransaction4.commit();

                    break;
            }
0

I solved the issue after calling commit() and again replacing the fragment you should start from

fragmentTransaction = fragmentManager.beginTransaction();

if(position==0){
     fragmentTransaction.replace(android.R.id.content, f1_fragment);
 else{
     fragmentTransaction.replace(android.R.id.content, f2_fragment);
 }

 fragmentTransaction.addToBackStack(null);
 fragmentTransaction.commit();
Fazal
  • 2,966
  • 1
  • 12
  • 20