title: Using a Fragment in Android 2.2
body: <p>I am using fragments for Android 2.2 (with backward compatibility) for the first time. I created a simple example to see how it works.</p>
  <p>Here is the code snapshot.</p>
  <p>Main Activity code:</p>
  <pre><code>public class FragmentExampleActivity extends  FragmentActivity {
  
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main_activity);
             
      
      RadioButton radioButton1 = (RadioButton)findViewById(R.id.radioButton1);
      radioButton1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
          
          public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
              changeView1();
          }
      });
      
      
      radioButton1.setOnClickListener(new OnClickListener() {
          
          public void onClick(View v) {
              //changeView1();
          }
      });
  }
  
  protected void changeView1() {
      // Create new fragment and transaction
      Fragment newFragment = new RadioActivity1();
      FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
      
        
  
      // Replace whatever is in the fragment_container view with this fragment,
      // and add the transaction to the back stack
      transaction.replace(R.id.relativeLayout, newFragment);
      transaction.addToBackStack(null);
  
      // Commit the transaction
      transaction.commit();
  }
  }
  </code></pre>
  <p>Main Activity Layout file (main_activity)</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  android:layout_width=&quot;fill_parent&quot;
  android:layout_height=&quot;fill_parent&quot; android:id=&quot;@+id/relativeLayout&quot;&gt;
  
  &lt;FrameLayout android:id=&quot;@+id/frameLayout&quot;  android:layout_width=&quot;fill_parent&quot;
      android:layout_height=&quot;fill_parent&quot; android:layout_alignParentTop=&quot;true&quot;
      android:layout_above=&quot;@+id/radioButtonGroupLayout&quot;&gt;
      
      &lt;fragment android:name=&quot;com.example.fragmentexample.HelloWorldFragment&quot; 
      android:id=&quot;@+id/generalView&quot; android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot; /&gt;
  &lt;/FrameLayout&gt;
  
  
  &lt;RadioGroup android:id =&quot;@+id/radioButtonGroupLayout&quot; android:layout_width=&quot;fill_parent&quot; android:layout_height=&quot;wrap_content&quot;
      android:orientation=&quot;horizontal&quot; android:layout_alignParentBottom=&quot;true&quot;&gt;
      
      &lt;RadioButton android:id=&quot;@+id/radioButton1&quot; android:text=&quot;Radio 1&quot; 
          android:layout_width=&quot;wrap_content&quot; android:layout_height=&quot;wrap_content&quot;/&gt;
      
      &lt;RadioButton android:id=&quot;@+id/radioButton2&quot; android:text=&quot;Radio 2&quot; 
          android:layout_width=&quot;wrap_content&quot; android:layout_height=&quot;wrap_content&quot;/&gt;
      
      &lt;RadioButton android:id=&quot;@+id/radioButton3&quot; android:text=&quot;Radio 3&quot; 
          android:layout_width=&quot;wrap_content&quot; android:layout_height=&quot;wrap_content&quot;/&gt;
  &lt;/RadioGroup&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  <p>Initial Fragment Class</p>
  <pre><code>public class HelloWorldFragment extends Fragment
  {
  
  public HelloWorldFragment() {
      
  }
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
          Bundle savedInstanceState) {
      // TODO Auto-generated method stub
      View v = inflater.inflate(R.layout.hello_world_fragment_layout,null);
      return v;
  }
  
  
  }
  </code></pre>
  <p>Fragment which is called when radio button is clicked/checked</p>
  <pre><code>public class RadioActivity1 extends Fragment {
  
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
          Bundle savedInstanceState) {
       View v = inflater.inflate(R.layout.radio_activity_one_layout,null);
          return v;
      
  }
  }
  </code></pre>
  <p>layout for second fragment class</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  android:layout_width=&quot;match_parent&quot;
  android:layout_height=&quot;match_parent&quot;
  android:orientation=&quot;vertical&quot; &gt;
  
  &lt;Button
      android:id=&quot;@+id/button1&quot;
      android:layout_width=&quot;wrap_content&quot;
      android:layout_height=&quot;wrap_content&quot;
      android:text=&quot;Button&quot; /&gt;
  
  &lt;TextView
      android:id=&quot;@+id/textView1&quot;
      android:layout_width=&quot;wrap_content&quot;
      android:layout_height=&quot;wrap_content&quot;
      android:text=&quot;Large Text&quot;
      android:textAppearance=&quot;?android:attr/textAppearanceLarge&quot; /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  <p>snapshot of code to replace the fragment</p>
  <pre><code>// Create new fragment and transaction
      Fragment newFragment = new RadioActivity1();
      FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
      
          transaction.remove(getSupportFragmentManager().findFragmentById(R.id.generalView));
  
      // Replace whatever is in the fragment_container view with this fragment,
      // and add the transaction to the back stack
      transaction.replace(R.id.relativeLayout, newFragment);
      transaction.addToBackStack(null);
  
      // Commit the transaction
      transaction.commit();
  </code></pre>
  <p>My problem is that the fragment is not getting replaced... I can see the HelloFragment Layout behind the second fragment.</p>