Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android FragmentTransaction commit already called

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();         }     }); 
like image 909
JPs Avatar asked Jul 03 '14 19:07

JPs


People also ask

How to replace one fragment with another in Android?

Use replace() to replace an existing fragment in a container with an instance of a new fragment class that you provide. Calling replace() is equivalent to calling remove() with a fragment in a container and adding a new fragment to that same container. transaction. commit();

How to detach fragment in Android?

To detach an added Fragment from an Activity, you use: getFragmentManager(). beginTransaction(). detach(mFragment). commit().


2 Answers

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();     } }); 
like image 140
Bryan Herbst Avatar answered Sep 21 '22 07:09

Bryan Herbst


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();  } 
like image 32
idris yıldız Avatar answered Sep 21 '22 07:09

idris yıldız