title: Android Fragment Duplication
body: <p>I'm trying out the new  class in android Honeycomb preview, and I've got a small issue. I am making a baseball scoring app, and I want the buttons on the left (Action Menu) to switch the "Action Pane" on the right, which I've set up as a fragment.</p>
  
  <p>I'd like the buttons' onClickListener() to call a Fragment Transaction to swap it out. So far it works, EXCEPT that when the app loads, it creates the default fragment, but when I hit a button, instead of REPLACING the default fragment, it creates a whole new one next to it.</p>
  
  <p>I've looked for hours, and I can't see what I'm doing wrong...</p>
  
  <p>Keep in mind I'm new to java/android/programming, so I might be missing something that's painfully obvious.</p>
  
  <h2>hc_test.java (main activity)</h2>
  
  <pre><code>package com.pte.hc_test;
  
  import android.app.Activity;
  import android.app.Fragment;
  import android.app.FragmentTransaction;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.Button;
  
  public class hc_test extends Activity {
      /** Called when the activity is first created. */
  
      /* declare class-level variables */
  //  private LinearLayout touchPad;
  //  private TextView touchCoordText;
  
      private Button pitchButton;
      private Button hitButton;
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
  
          pitchButton = (Button)findViewById(R.id.actionButton1);
          hitButton = (Button)findViewById(R.id.actionButton2);
  
          pitchButton.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  swapFragment(actionType.PITCH_ACTION);
              }
          });
  
          hitButton.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  swapFragment(actionType.HIT_ACTION);
              }
          });
      }
  
      private void swapFragment(int myType){
  
          Fragment f = new actionFragment(myType);
  
          // Execute a transaction, replacing any existing
          // fragment with this one inside the frame.
          FragmentTransaction ft = getFragmentManager().beginTransaction();
          ft.replace(R.id.actionFragment, f);
          ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
          ft.addToBackStack(null);
          ft.commit();
  
      }
  }
  </code></pre>
  
  <h2>main.xml</h2>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  
  &lt;!-- this is the parent layout of the whole screen --&gt;
  &lt;LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:background="#2F2F4F"
      &gt;
  
      &lt;!-- SCOREBOARD MASTER LAYOUT --&gt;
      &lt;LinearLayout
          android:orientation="horizontal"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          &gt;
  
          &lt;!-- team name placeholders --&gt;
          &lt;LinearLayout
              android:orientation="vertical"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_weight="0"
              android:padding="5dp"
              &gt;
              &lt;TextView style="@style/inningLabel"
                  android:text="TEAM"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  /&gt;
              &lt;TextView style="@style/inningValue"
                  android:text="@string/visitor"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  /&gt;
              &lt;TextView style="@style/inningValue"
                  android:text="@string/home"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  /&gt;
          &lt;/LinearLayout&gt;
          &lt;!-- end team names --&gt;
  
          &lt;!-- Inning table --&gt;
          &lt;HorizontalScrollView
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:layout_weight="1"
              android:fadingEdge="vertical"&gt;
  
              &lt;TableLayout
                  android:id = "@+id/innings"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:padding="5dp"
                  &gt;
                  &lt;!-- inning label row --&gt;
  
                  &lt;!-- get these done with code?? --&gt;
                  &lt;TableRow android:layout_height="wrap_content"&gt;
                      &lt;TextView style="@style/inningLabel"
                          android:id="@+id/inn1_label"
                          android:text="1"
                          android:layout_height="wrap_content"
                          /&gt;
                      &lt;TextView style="@style/inningLabel"
                          android:id="@+id/inn2_label"
                          android:text="2"
                          android:layout_height="wrap_content"
                          /&gt;
                      &lt;TextView style="@style/inningLabel"
                          android:id="@+id/inn3_label"
                          android:text="3"
                          android:layout_height="wrap_content"
                          /&gt;
                      &lt;TextView style="@style/inningLabel"
                          android:id="@+id/inn4_label"
                          android:text="4"
                          android:layout_height="wrap_content"
                          /&gt;
                      &lt;TextView style="@style/inningLabel"
                          android:id="@+id/inn5_label"
                          android:text="5"
                          android:layout_height="wrap_content"
                          /&gt;
                      &lt;TextView style="@style/inningLabel"
                          android:id="@+id/inn6_label"
                          android:text="6"
                          android:layout_height="wrap_content"
                          /&gt;
                      &lt;TextView style="@style/inningLabel"
                          android:id="@+id/inn7_label"
                          android:text="7"
                          android:layout_height="wrap_content"
                          /&gt;
                      &lt;TextView style="@style/inningLabel"
                          android:id="@+id/inn8_label"
                          android:text="8"
                          android:layout_height="wrap_content"
                          /&gt;
                      &lt;TextView style="@style/inningLabel"
                          android:id="@+id/inn9_label"
                          android:text="9"
                          android:layout_height="wrap_content"
                          /&gt;
                  &lt;/TableRow&gt;
                  &lt;!-- end inning labels --&gt;
  
                  &lt;!-- top inning row --&gt;
                  &lt;TableRow android:layout_height="wrap_content"&gt;
                      &lt;TextView style="@style/inningValue"
                          android:id="@+id/top1_label"
                          android:text="0"
                          android:layout_height="wrap_content"
                          /&gt;
                      &lt;TextView style="@style/inningValue"
                          android:id="@+id/top2_label"
                          android:text="0"
                          android:layout_height="wrap_content"
                          /&gt;
                      &lt;TextView style="@style/inningValue"
                          android:id="@+id/top3_label"
                          android:text="0"
                          android:layout_height="wrap_content"
                          /&gt;
                      &lt;TextView style="@style/inningValue"
                          android:id="@+id/top4_label"
                          android:text="0"
                          android:layout_height="wrap_content"
                          /&gt;
                      &lt;TextView style="@style/inningValue"
                          android:id="@+id/top5_label"
                          android:text="0"
                          android:layout_height="wrap_content"
                          /&gt;
                      &lt;TextView style="@style/inningValue"
                          android:id="@+id/top6_label"
                          android:text="0"
                          android:layout_height="wrap_content"
                          /&gt;
                      &lt;TextView style="@style/inningValue"
                          android:id="@+id/top7_label"
                          android:text="0"
                          android:layout_height="wrap_content"
                          /&gt;
                      &lt;TextView style="@style/inningValue"
                          android:id="@+id/top8_label"
                          android:text="0"
                          android:layout_height="wrap_content"
                          /&gt;
                      &lt;TextView style="@style/inningValue"
                          android:id="@+id/top9_label"
                          android:text="0"
                          android:layout_height="wrap_content"
                          /&gt;
                  &lt;/TableRow&gt;
                  &lt;!-- end top inning row --&gt;
  
                  &lt;!-- bottom inning row --&gt;
                  &lt;TableRow android:layout_height="wrap_content"&gt;
                      &lt;TextView style="@style/inningValue"
                          android:id="@+id/bot1_label"
                          android:text="0"
                          android:layout_height="wrap_content"
                          /&gt;
                      &lt;TextView style="@style/inningValue"
                          android:id="@+id/bot2_label"
                          android:text="0"
                          android:layout_height="wrap_content"
                          /&gt;
                      &lt;TextView style="@style/inningValue"
                          android:id="@+id/bot3_label"
                          android:text="0"
                          android:layout_height="wrap_content"
                          /&gt;
                      &lt;TextView style="@style/inningValue"
                          android:id="@+id/bot4_label"
                          android:text="0"
                          android:layout_height="wrap_content"
                          /&gt;
                      &lt;TextView style="@style/inningValue"
                          android:id="@+id/bot5_label"
                          android:text="0"
                          android:layout_height="wrap_content"
                          /&gt;
                      &lt;TextView style="@style/inningValue"
                          android:id="@+id/bot6_label"
                          android:text="0"
                          android:layout_height="wrap_content"
                          /&gt;
                      &lt;TextView style="@style/inningValue"
                          android:id="@+id/bot7_label"
                          android:text="0"
                          android:layout_height="wrap_content"
                          /&gt;
                      &lt;TextView style="@style/inningValue"
                          android:id="@+id/bot8_label"
                          android:text="0"
                          android:layout_height="wrap_content"
                          /&gt;
                      &lt;TextView style="@style/inningValue"
                          android:id="@+id/bot9_label"
                          android:text="0"
                          android:layout_height="wrap_content"
                          /&gt;
                  &lt;/TableRow&gt;
                  &lt;!-- end bottom inning row --&gt;
  
              &lt;/TableLayout&gt;
          &lt;/HorizontalScrollView&gt;
          &lt;!-- end inning table --&gt;
  
          &lt;!-- Runs, Hits, Errors Count --&gt;
          &lt;TableLayout
              android:id = "@+id/RHE"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_weight="0"
              android:padding="5dp"&gt;
  
              &lt;TableRow android:layout_height="wrap_content"&gt;
                  &lt;TextView style="@style/inningLabel"
                      android:id="@+id/runLabel"
                      android:text="R"
                      android:layout_height="wrap_content"
                      /&gt;
                  &lt;TextView style="@style/inningLabel"
                      android:id="@+id/hitLabel"
                      android:text="H"
                      android:layout_height="wrap_content"
                      /&gt;
                  &lt;TextView style="@style/inningLabel"
                      android:id="@+id/errorLabel"
                      android:text="E"
                      android:layout_height="wrap_content"
                      /&gt;
              &lt;/TableRow&gt;
  
              &lt;TableRow android:layout_height="wrap_content"&gt;
                  &lt;TextView style="@style/inningValue"
                      android:id="@+id/visitorRuns"
                      android:text="0"
                      android:layout_height="wrap_content"
                      /&gt;
                  &lt;TextView style="@style/inningValue"
                      android:id="@+id/visitorHits"
                      android:text="0"
                      android:layout_height="wrap_content"
                      /&gt;
                  &lt;TextView style="@style/inningValue"
                      android:id="@+id/visitorErrors"
                      android:text="0"
                      android:layout_height="wrap_content"
                      /&gt;
              &lt;/TableRow&gt;
  
              &lt;TableRow android:layout_height="wrap_content"&gt;
                  &lt;TextView style="@style/inningValue"
                      android:id="@+id/homeRuns"
                      android:text="0"
                      android:layout_height="wrap_content"
                      /&gt;
                  &lt;TextView style="@style/inningValue"
                      android:id="@+id/homeHits"
                      android:text="0"
                      android:layout_height="wrap_content"
                      /&gt;
                  &lt;TextView style="@style/inningValue"
                      android:id="@+id/homeErrors"
                      android:text="0"
                      android:layout_height="wrap_content"
                      /&gt;
              &lt;/TableRow&gt;
  
          &lt;/TableLayout&gt;
  
      &lt;/LinearLayout&gt;
      &lt;!-- END OF SCOREBOARD LAYOUT --&gt;
  
      &lt;!-- MAIN ACTIVITY SECTION --&gt;
      &lt;LinearLayout
          android:orientation="horizontal"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"&gt;
  
          &lt;!-- ACTION MENU BEGIN --&gt;
          &lt;LinearLayout
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:layout_weight="1"&gt;
  
              &lt;Button
                  android:id="@+id/actionButton1"
                  android:text="ActionButton1"
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  /&gt;
              &lt;Button
                  android:id="@+id/actionButton2"
                  android:text="ActionButton2"
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  /&gt;
              &lt;Button
                  android:id="@+id/actionButton3"
                  android:text="ActionButton3"
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  /&gt;
              &lt;Button
                  android:id="@+id/actionButton4"
                  android:text="ActionButton4"
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  /&gt;
              &lt;Button
                  android:id="@+id/actionButton5"
                  android:text="ActionButton5"
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  /&gt;
  
          &lt;/LinearLayout&gt;
          &lt;!-- ACTION MENU END --&gt;
  
          &lt;!-- ACTION FRAME BEGIN --&gt;
  
          &lt;fragment class="com.pte.hc_test.actionFragment"
              android:id="@+id/actionFragment"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:layout_weight="1" /&gt;
  
          &lt;!-- ACTION FRAME END --&gt;
  
      &lt;/LinearLayout&gt;
      &lt;!-- MAIN ACTIVITY SECTION END --&gt;
  
      &lt;!-- LIVE STATS FRAME BEGIN --&gt;
      &lt;LinearLayout
          android:orientation="horizontal"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"&gt;
  
          &lt;!-- left live stat pane --&gt;
          &lt;LinearLayout
              android:id="@+id/left_stat_pane"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:layout_weight="1"
              &gt;
  
              &lt;TextView style="@style/statHeader"
                  android:id="@+id/left_playerName"
                  android:text="{PITCHER NAME}"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  /&gt;
  
              &lt;TextView style="@style/statText"
                  android:id="@+id/left_stat1"
                  android:text="Stat1"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  /&gt;
              &lt;TextView style="@style/statText"
                  android:id="@+id/left_stat2"
                  android:text="Stat2"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  /&gt;
              &lt;TextView style="@style/statText"
                  android:id="@+id/left_stat3"
                  android:text="Stat3"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  /&gt;
          &lt;/LinearLayout&gt;
          &lt;!-- end left live stat pane --&gt;
  
          &lt;!-- right live stat pane --&gt;
          &lt;LinearLayout
              android:id="@+id/right_stat_pane"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:layout_weight="1"
              &gt;
  
              &lt;TextView style="@style/statHeader"
                  android:id="@+id/right_playerName"
                  android:text="{BATTER NAME}"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  /&gt;
  
              &lt;TextView style="@style/statText"
                  android:id="@+id/right_stat1"
                  android:text="Stat1"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  /&gt;
              &lt;TextView style="@style/statText"
                  android:id="@+id/right_stat2"
                  android:text="Stat2"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  /&gt;
              &lt;TextView style="@style/statText"
                  android:id="@+id/right_stat3"
                  android:text="Stat3"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  /&gt;
  
          &lt;/LinearLayout&gt;
          &lt;!-- end right live stat pane --&gt;
  
      &lt;/LinearLayout&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <h2>actionFragment.java</h2>
  
  <pre><code>package com.pte.hc_test;
  
  import android.app.Fragment;
  import android.content.Context;
  import android.os.Bundle;
  import android.util.Log;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.ImageView;
  import android.widget.LinearLayout;
  import android.widget.TableLayout;
  import android.widget.TableRow;
  import android.widget.TextView;
  
  public class actionFragment extends Fragment {
  
      // class variables
      int mActivityType;
  
      // default (null) constructor
      public actionFragment(){
          Log.v("PTE", "null constructor");       
      }
  
      public actionFragment(int n){
          mActivityType = n;
          Log.v("PTE", "explicit constructor (" + n + ")");
      }
  
      @Override
      public void onCreate(Bundle saved){
          super.onCreate(saved);
          if (saved != null){
              mActivityType = saved.getInt("Type");
          }
          Log.v("PTE", "FIRE: actionFragment.onCreate()");
      }
  
      @Override
      public void onSaveInstanceState(Bundle toSave){
          toSave.putInt("Type", mActivityType);
          Log.v("PTE", "FIRE: actionFragment.onSaveInstanceState()");
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
  
          Log.v("PTE", "FIRE: actionFragment.onCreateView()");
          Log.v("PTE", "with mActivityType == " + mActivityType);
          try {
              Log.v("PTE", "with onCreateView()'s container = " + container.toString());
          } catch (Exception e) {
              Log.v("PTE", "could not convert container to string. Must be null");
              Log.v("PTE", e.toString());
          }
  
          Context c = getActivity().getApplicationContext();
          LinearLayout actionPane = new LinearLayout(c);
  
          switch (mActivityType) {
              case actionType.PITCH_ACTION:
                  Log.v("PTE", "FIRE: actionFragment.pitchPane()");
  
                  // instantiate all the required views
                   // parent container
                  TextView topLabel = new TextView(c);
                  TableLayout strikeZone = new TableLayout(c);
                  ImageView image = new ImageView(c);
  
                  // set properties for each view
                  actionPane.setOrientation(LinearLayout.VERTICAL);
  
                  topLabel.setText("top Label Text");
  
                  // create the strike zone table
                  for(int i=1; i&lt;6; i++){
                      TableRow tr = new TableRow(c);
  
                      for(int j=1; j&lt;6; j++){
                          TextView tv = new TextView(c);
                          tv.setText("C" + j + ":R" + i);
                          tv.setPadding(3, 3, 3, 3);
                          tr.addView(tv);
                      } 
                      strikeZone.addView(tr);
                  }
  
                  image.setPadding(0, 60, 0, 30);
                  image.setImageResource(R.drawable.homeplate);
  
                  // add child views to parent
                  actionPane.addView(topLabel);
                  actionPane.addView(strikeZone);
                  actionPane.addView(image);
  
                  return actionPane;
  
              case actionType.HIT_ACTION:
                  Log.v("PTE", "FIRE: actionFragment.hitPane()");
  
                  // simple layout with a text view for testing
                  TextView placeholder = new TextView(c);
                  placeholder.setText("This is a placeholder");
  
                  actionPane.addView(placeholder);
                  return actionPane;
  
              default:
                  Log.v("PTE", "FIRE: actionFragment.defaultPane()");                 
  
                              // If I comment this TextView out, I achieve my intended behavior
                  TextView label = new TextView(c);
                  label.setText("This is the default pane");
                  actionPane.addView(label);
  
                  return actionPane;
          }
      }
  
  }
  </code></pre>