title: How to solve illegal state exception while removing fragment in android
body: <p>I am adding and removing fragment dynamically in android while removing fragment .I am getting this exception can anybody tell how to remove fragment?</p>
  
  <pre><code>12-09 15:00:16.363: ERROR/AndroidRuntime(673): FATAL EXCEPTION: main
  12-09 15:00:16.363: ERROR/AndroidRuntime(673): java.lang.IllegalStateException: commit already called
  12-09 15:00:16.363: ERROR/AndroidRuntime(673):     at android.app.BackStackRecord.commitInternal(BackStackRecord.java:499)
  12-09 15:00:16.363: ERROR/AndroidRuntime(673):     at android.app.BackStackRecord.commit(BackStackRecord.java:491)
  12-09 15:00:16.363: ERROR/AndroidRuntime(673):     at com.gcm.fragment.GcmFragmentActivity.onClick(GcmFragmentActivity.java:50)
  12-09 15:00:16.363: ERROR/AndroidRuntime(673):     at android.view.View.performClick(View.java:3110)
  12-09 15:00:16.363: ERROR/AndroidRuntime(673):     at android.view.View$PerformClick.run(View.java:11928)
  12-09 15:00:16.363: ERROR/AndroidRuntime(673):     at android.os.Handler.handleCallback(Handler.java:587)
  12-09 15:00:16.363: ERROR/AndroidRuntime(673):     at android.os.Handler.dispatchMessage(Handler.java:92)
  12-09 15:00:16.363: ERROR/AndroidRuntime(673):     at android.os.Looper.loop(Looper.java:132)
  12-09 15:00:16.363: ERROR/AndroidRuntime(673):     at android.app.ActivityThread.main(ActivityThread.java:4025)
  12-09 15:00:16.363: ERROR/AndroidRuntime(673):     at java.lang.reflect.Method.invokeNative(Native Method)
  12-09 15:00:16.363: ERROR/AndroidRuntime(673):     at java.lang.reflect.Method.invoke(Method.java:491)
  12-09 15:00:16.363: ERROR/AndroidRuntime(673):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
  12-09 15:00:16.363: ERROR/AndroidRuntime(673):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
  12-09 15:00:16.363: ERROR/AndroidRuntime(673):     at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>My code is below</p>
  
  <pre><code>package com.gcm.fragment;
  import android.app.Activity;
  import android.app.Fragment;
  import android.app.FragmentManager;
  import android.app.FragmentTransaction;
  import android.os.Bundle;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.TextView;
  
  public class GcmFragmentActivity extends Activity implements OnClickListener {
      TextView txtIhaveIdea=null;
      boolean subMenu=false;
      Fragment fragOne = new SubFragment(); 
      FragmentManager fm = getFragmentManager(); 
      FragmentTransaction ft = fm.beginTransaction(); 
  
      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
          txtIhaveIdea=(TextView)findViewById(R.id.txtIhaveAnIdea);
          txtIhaveIdea.setOnClickListener(this);
  
          ; 
      }
  
      @Override
      public void onClick(View v) {
          // TODO Auto-generated method stub
          if(v.getId()==R.id.txtIhaveAnIdea)
          {
              if(!subMenu)
              {
  
                  /*Bundle arguments = new Bundle(); 
                  arguments.putBoolean("shouldYouCreateAChildFragment", true); 
                  fragOne.setArguments(arguments); */
                  //ft.show(fragOne);
                  fm.beginTransaction(); 
                  ft.add(R.id.main_frag_container, fragOne);              
                  ft.commit();
                  subMenu=true;
              }
              else if(subMenu)
              {
                  fm.beginTransaction(); 
                  ft.remove(fragOne);
                  ft.commit();
                  /*fm.beginTransaction(); 
                  ft.replace(R.id.main_frag_container, null);                 
                  ft.commit();*/
                  subMenu=false;
                  //ft.hide(fragOne);
  
                  //ft.remove(fragOne);
                  /*FragmentManager fm = getFragmentManager(); 
                  FragmentTransaction ft = fm.beginTransaction(); 
  
                  fm.beginTransaction(); 
                  Fragment fragOne = new SubFragment(); 
                  Bundle arguments = new Bundle(); 
                  arguments.putBoolean("shouldYouCreateAChildFragment", true); 
                  fragOne.setArguments(arguments); 
                  ft.remove(fragOne); 
                  ft.commit();    */
              }
  
          }
  
      }
  }
  
  /*FragmentManager fm = getFragmentManager(); 
  FragmentTransaction ft = fm.beginTransaction(); 
  
  fm.beginTransaction(); 
  Fragment fragOne = new MyFragment(); 
  Bundle arguments = new Bundle(); 
  arguments.putBoolean("shouldYouCreateAChildFragment", true); 
  fragOne.setArguments(arguments); 
  ft.add(R.id.main_frag_container, fragOne); 
  ft.commit(); 
  */
  </code></pre>
  
  <p>can anybody tell how to fix the problem</p>
  
  <p>Thanks</p>