Home | History | Annotate | Download | only in fragments
      1 page.title=Building a Flexible UI
      2 
      3 trainingnavtop=true
      4 
      5 @jd:body
      6 
      7 <div id="tb-wrapper">
      8   <div id="tb"> 
      9     <h2>This lesson teaches you to</h2>
     10 <ol>
     11   <li><a href="#AddAtRuntime">Add a Fragment to an Activity at Runtime</a></li>
     12   <li><a href="#Replace">Replace One Fragment with Another</a></li>
     13 </ol>
     14     
     15     <h2>You should also read</h2>
     16     <ul>
     17       <li><a href="{@docRoot}guide/components/fragments.html">Fragments</a></li>
     18       <li><a href="{@docRoot}guide/practices/tablets-and-handsets.html">Supporting Tablets and
     19 Handsets</a></li>
     20     </ul>
     21 
     22 <h2>Try it out</h2>
     23 
     24 <div class="download-box">
     25  <a href="http://developer.android.com/shareables/training/FragmentBasics.zip"
     26 class="button">Download the sample</a>
     27  <p class="filename">FragmentBasics.zip</p>
     28 </div>
     29 
     30   </div>
     31 </div>
     32 
     33 
     34 <p>When designing your application to support a wide range of screen sizes, you can reuse your
     35 fragments in different layout configurations to optimize the user experience based on the available
     36 screen space.</p>
     37 
     38 <p>For example, on a handset device it might be appropriate to display just one fragment at a time
     39 for a single-pane user interface.  Conversely, you may want to set fragments side-by-side on a
     40 tablet which has a wider screen size to display more information to the user.</p>
     41 
     42 <img src="{@docRoot}images/training/basics/fragments-screen-mock.png" alt="" />
     43 <p class="img-caption"><strong>Figure 1.</strong> Two fragments, displayed in different
     44 configurations for the same activity on different screen sizes. On a large screen, both fragments
     45 fit side by side, but on a handset device, only one fragment fits at a time so the fragments must
     46 replace each other as the user navigates.</p>
     47 
     48 <p>The {@link android.support.v4.app.FragmentManager} class provides methods that allow you to add,
     49 remove, and replace fragments to an activity at runtime in order to create a dynamic experience.</p>
     50 
     51 
     52 
     53 <h2 id="AddAtRuntime">Add a Fragment to an Activity at Runtime</h2> 
     54 
     55 <p>Rather than defining the fragments for an activity in the layout file&mdash;as shown in the
     56 <a href="creating.html">previous lesson</a> with the {@code &lt;fragment>} element&mdash;you can add
     57 a fragment to the activity during the activity runtime. This is necessary
     58 if you plan to change fragments during the life of the activity.</p>
     59 
     60 <p>To perform a transaction such as add or 
     61 remove a fragment, you must use the {@link android.support.v4.app.FragmentManager} to create a
     62 {@link android.support.v4.app.FragmentTransaction}, which provides APIs to add, remove, replace,
     63 and perform other fragment transactions.</p>
     64 
     65 <p>If your activity allows the fragments to be removed and replaced, you should add the
     66 initial fragment(s) to the activity during the activity's
     67 {@link android.app.Activity#onCreate onCreate()} method.</p>
     68 
     69 <p>An important rule when dealing with fragments&mdash;especially when adding fragments at
     70 runtime&mdash;is that your activity layout must include a container {@link android.view.View}
     71 in which you can insert the fragment.</p>
     72 
     73 <p>The following layout is an alternative to the layout shown in the <a
     74 href="creating.html">previous lesson</a> that shows only one fragment at a time. In order to replace
     75 one fragment with another, the activity's layout
     76 includes an empty {@link android.widget.FrameLayout} that acts as the fragment container.</p>
     77 
     78 <p>Notice that the filename is the same as the layout file in the previous lesson, but the layout
     79 directory does <em>not</em> have the <code>large</code> qualifier, so this layout is used when the
     80 device screen is smaller than <em>large</em> because the screen does not fit both fragments at
     81 the same time.</p>
     82 
     83 <p><code>res/layout/news_articles.xml:</code></p>
     84 <pre>
     85 &lt;FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
     86     android:id="@+id/fragment_container"
     87     android:layout_width="match_parent"
     88     android:layout_height="match_parent" />
     89 </pre>
     90 
     91 <p>Inside your activity, call {@link
     92 android.support.v4.app.FragmentActivity#getSupportFragmentManager()} to get a {@link
     93 android.support.v4.app.FragmentManager} using the Support Library APIs. Then call {@link
     94 android.support.v4.app.FragmentManager#beginTransaction} to create a {@link
     95 android.support.v4.app.FragmentTransaction} and call {@link
     96 android.support.v4.app.FragmentTransaction#add add()} to add a fragment.</p>
     97 
     98 <p>You can perform multiple fragment transaction for the activity using the same {@link
     99 android.support.v4.app.FragmentTransaction}. When you're ready to make the changes, you must call
    100 {@link android.support.v4.app.FragmentTransaction#commit()}.</p>
    101 
    102 <p>For example, here's how to add a fragment to the previous layout:</p>
    103 
    104 <pre>
    105 import android.os.Bundle;
    106 import android.support.v4.app.FragmentActivity;
    107 
    108 public class MainActivity extends FragmentActivity {
    109     &#64;Override
    110     public void onCreate(Bundle savedInstanceState) {
    111         super.onCreate(savedInstanceState);
    112         setContentView(R.layout.news_articles);
    113 
    114         // Check that the activity is using the layout version with
    115         // the fragment_container FrameLayout
    116         if (findViewById(R.id.fragment_container) != null) {
    117 
    118             // However, if we're being restored from a previous state,
    119             // then we don't need to do anything and should return or else
    120             // we could end up with overlapping fragments.
    121             if (savedInstanceState != null) {
    122                 return;
    123             }
    124 
    125             // Create a new Fragment to be placed in the activity layout
    126             HeadlinesFragment firstFragment = new HeadlinesFragment();
    127             
    128             // In case this activity was started with special instructions from an
    129             // Intent, pass the Intent's extras to the fragment as arguments
    130             firstFragment.setArguments(getIntent().getExtras());
    131             
    132             // Add the fragment to the 'fragment_container' FrameLayout
    133             getSupportFragmentManager().beginTransaction()
    134                     .add(R.id.fragment_container, firstFragment).commit();
    135         }
    136     }
    137 }
    138 </pre>
    139 
    140 <p>Because the fragment has been added to the {@link android.widget.FrameLayout} container at
    141 runtime&mdash;instead of defining it in the activity's layout with a {@code &lt;fragment>}
    142 element&mdash;the activity can remove the fragment and replace it with a different one.</p>
    143 
    144 
    145 
    146 <h2 id="Replace">Replace One Fragment with Another</h2>
    147 
    148 <p>The procedure to replace a fragment is similar to adding one, but requires the {@link
    149 android.support.v4.app.FragmentTransaction#replace replace()} method instead of {@link
    150 android.support.v4.app.FragmentTransaction#add add()}.</p>
    151 
    152 <p>Keep in mind that when you perform fragment transactions, such as replace or remove one, it's
    153 often appropriate to allow the user to navigate backward and "undo" the change. To allow the user
    154 to navigate backward through the fragment transactions, you must call {@link
    155 android.support.v4.app.FragmentTransaction#addToBackStack addToBackStack()} before you commit the
    156 {@link android.support.v4.app.FragmentTransaction}.</p>
    157 
    158 <p class="note"><strong>Note:</strong> When you remove or replace a fragment and add the transaction
    159 to the back stack, the fragment that is removed is stopped (not destroyed). If the user navigates
    160 back to restore the fragment, it restarts. If you <em>do not</em> add the transaction to the back
    161 stack, then the fragment is destroyed when removed or replaced.</p>
    162 
    163 <p>Example of replacing one fragment with another:</p>
    164 
    165 <pre>
    166 // Create fragment and give it an argument specifying the article it should show
    167 ArticleFragment newFragment = new ArticleFragment();
    168 Bundle args = new Bundle();
    169 args.putInt(ArticleFragment.ARG_POSITION, position);
    170 newFragment.setArguments(args);
    171 
    172 FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    173 
    174 // Replace whatever is in the fragment_container view with this fragment,
    175 // and add the transaction to the back stack so the user can navigate back
    176 transaction.replace(R.id.fragment_container, newFragment);
    177 transaction.addToBackStack(null);
    178 
    179 // Commit the transaction
    180 transaction.commit();
    181 </pre>
    182 
    183 <p>The {@link android.support.v4.app.FragmentTransaction#addToBackStack addToBackStack()} method
    184 takes an optional string parameter that specifies a unique name for the transaction. The name isn't
    185 needed unless you plan to perform advanced fragment operations using the {@link
    186 android.support.v4.app.FragmentManager.BackStackEntry} APIs.</p>
    187 
    188 
    189 
    190 
    191