title: Replacing Android Fragments Dynamically
body: <p>I have just started playing around with Fragments and I am trying to dynamically add/replace fragments.</p>
  
  <p>I can add them just fine, like so...</p>
  
  <pre><code>@Override
  protected void onCreate(Bundle savedInstanceState)
  {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_home);
  
      FragmentManager fragMan = getSupportFragmentManager();
      FragmentTransaction fragTran = fragMan.beginTransaction();
  
      FragOne fOne = new FragOne();
      FragTwo fTwo = new FragTwo();
      FragThree fThree = new FragThree();
  
      fragTran.add(R.id.frag_cont_one, fOne);
      fragTran.add(R.id.frag_cont_two, fTwo);
      fragTran.commit();
  
  
  
  }
  </code></pre>
  
  <p>But when i try to replace fTwo with fThree the application crashes. Heres how i am doing the replace</p>
  
  <pre><code>fragTran.replace(R.id.frag_cont_two, fThree);
  fragTran.addToBackStack(null);
  fragTran.commit();
  </code></pre>
  
  <p>Any ideas? </p>
  
  <p>Thanks</p>
  
  <pre><code>10-08 21:30:44.710: E/Trace(3219): error opening trace file: No such file or directory (2)
  10-08 21:30:44.869: D/AndroidRuntime(3219): Shutting down VM
  10-08 21:30:44.869: W/dalvikvm(3219): threadid=1: thread exiting with uncaught exception         (group=0x40a71930)
  10-08 21:30:44.889: E/AndroidRuntime(3219): FATAL EXCEPTION: main
  10-08 21:30:44.889: E/AndroidRuntime(3219): java.lang.RuntimeException: Unable to start activity ComponentInfo{evans.louis.fragments/evans.louis.fragments.Home}: java.lang.IllegalStateException: commit already called
  10-08 21:30:44.889: E/AndroidRuntime(3219):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
  10-08 21:30:44.889: E/AndroidRuntime(3219):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
  10-08 21:30:44.889: E/AndroidRuntime(3219):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
  10-08 21:30:44.889: E/AndroidRuntime(3219):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
  10-08 21:30:44.889: E/AndroidRuntime(3219):     at android.os.Handler.dispatchMessage(Handler.java:99)
  10-08 21:30:44.889: E/AndroidRuntime(3219):     at android.os.Looper.loop(Looper.java:137)
  10-08 21:30:44.889: E/AndroidRuntime(3219):     at android.app.ActivityThread.main(ActivityThread.java:5041)
  10-08 21:30:44.889: E/AndroidRuntime(3219):     at java.lang.reflect.Method.invokeNative(Native Method)
  10-08 21:30:44.889: E/AndroidRuntime(3219):     at java.lang.reflect.Method.invoke(Method.java:511)
  10-08 21:30:44.889: E/AndroidRuntime(3219):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
  10-08 21:30:44.889: E/AndroidRuntime(3219):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
  10-08 21:30:44.889: E/AndroidRuntime(3219):     at dalvik.system.NativeStart.main(Native Method)
  10-08 21:30:44.889: E/AndroidRuntime(3219): Caused by: java.lang.IllegalStateException: commit already called
  10-08 21:30:44.889: E/AndroidRuntime(3219):     at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:582)
  10-08 21:30:44.889: E/AndroidRuntime(3219):     at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:574)
  10-08 21:30:44.889: E/AndroidRuntime(3219):     at evans.louis.fragments.Home.onCreate(Home.java:37)
  10-08 21:30:44.889: E/AndroidRuntime(3219):     at android.app.Activity.performCreate(Activity.java:5104)
  10-08 21:30:44.889: E/AndroidRuntime(3219):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
  10-08 21:30:44.889: E/AndroidRuntime(3219):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
  10-08 21:30:44.889: E/AndroidRuntime(3219):     ... 11 more
  </code></pre>
  
  <p>EDIT: LogCat added</p>