Home | History | Annotate | Download | only in fragments
      1 page.title=Creating a Fragment
      2 parent.title=Building a Dynamic UI with Fragments
      3 parent.link=index.html
      4 
      5 trainingnavtop=true
      6 previous.title=Using the Android Support Library
      7 previous.link=support-lib.html
      8 next.title=Building a Flexible UI
      9 next.link=fragment-ui.html
     10 
     11 @jd:body
     12 
     13 <div id="tb-wrapper">
     14   <div id="tb">
     15     
     16     <h2>This lesson teaches you to</h2>
     17 <ol>
     18   <li><a href="#Create">Create a Fragment Class</a></li>
     19   <li><a href="#AddInLayout">Add a Fragment to an Activity using XML</a></li>
     20 </ol>
     21 
     22     <h2>You should also read</h2>
     23     <ul>
     24       <li><a href="{@docRoot}guide/components/fragments.html">Fragments</a></li>
     25     </ul>
     26 
     27 <h2>Try it out</h2>
     28     
     29 <div class="download-box">
     30  <a href="http://developer.android.com/shareables/training/FragmentBasics.zip"
     31 class="button">Download the sample</a>
     32  <p class="filename">FragmentBasics.zip</p>
     33 </div>
     34 
     35   </div>
     36 </div>
     37 
     38 <p>You can think of a fragment as a modular section of an activity, which has its own lifecycle,
     39 receives its own input events, and which you can add or remove while the activity is running (sort
     40 of like a "sub activity" that you can reuse in different activities). This lesson shows how to
     41 extend the {@link android.support.v4.app.Fragment} class using the Support Library so your app
     42 remains compatible with devices running system versions as old as Android 1.6.</p>
     43 
     44 <p class="note"><strong>Note:</strong> If you decide for other reasons that the minimum
     45 API level your app requires is 11 or higher, you don't need to use the Support
     46 Library and can instead use the framework's built in {@link android.app.Fragment} class and related
     47 APIs. Just be aware that this lesson is focused on using the APIs from the Support Library, which
     48 use a specific package signature and sometimes slightly different API names than the versions
     49 included in the platform.</p>
     50 
     51 
     52 
     53 <h2 id="Create">Create a Fragment Class</h2>
     54 
     55 <p>To create a fragment, extend the {@link android.support.v4.app.Fragment} class, then override 
     56 key lifecycle methods to insert your app logic, similar to the way you would with an {@link
     57 android.app.Activity} class.</p>
     58 
     59 <p>One difference when creating a {@link android.support.v4.app.Fragment} is that you must use the
     60 {@link android.support.v4.app.Fragment#onCreateView onCreateView()} callback to define the layout.
     61 In fact, this is the only callback you need in order to get a fragment running. For
     62 example, here's a simple fragment that specifies its own layout:</p>
     63 
     64 <pre>
     65 import android.os.Bundle;
     66 import android.support.v4.app.Fragment;
     67 import android.view.LayoutInflater;
     68 import android.view.ViewGroup;
     69 
     70 public class ArticleFragment extends Fragment {
     71     &#64;Override
     72     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     73         Bundle savedInstanceState) {
     74         // Inflate the layout for this fragment
     75         return inflater.inflate(R.layout.article_view, container, false);
     76     }
     77 }
     78 </pre>
     79 
     80 <p>Just like an activity, a fragment should implement other lifecycle callbacks that allow you to
     81 manage its state as it is added or removed from the activity and as the activity transitions
     82 between its lifecycle states. For instance, when the activity's {@link
     83 android.app.Activity#onPause()} method is called, any fragments in the activity also receive a call
     84 to {@link android.support.v4.app.Fragment#onPause()}.</p>
     85 
     86 <p>More information about the fragment lifecycle and callback methods is available in the <a
     87 href="{@docRoot}guide/components/fragments.html">Fragments</a> developer guide.</p>
     88 
     89 
     90 
     91 <h2 id="AddInLayout">Add a Fragment to an Activity using XML</h2> 
     92 
     93 <p>While fragments are reusable, modular UI components, each instance of a {@link
     94 android.support.v4.app.Fragment} class must be associated with a parent {@link
     95 android.support.v4.app.FragmentActivity}. You can achieve this association by defining each
     96 fragment within your activity layout XML file.</p>
     97 
     98 <p class="note"><strong>Note:</strong> {@link android.support.v4.app.FragmentActivity} is a
     99 special activity provided in the Support Library to handle fragments on system versions older than
    100 API level 11. If the lowest system version you support is API level 11 or higher, then you can use a
    101 regular {@link android.app.Activity}.</p>
    102 
    103 <p>Here is an example layout file that adds two fragments to an activity when the device
    104 screen is considered "large" (specified by the <code>large</code> qualifier in the directory
    105 name).</p>
    106 
    107 <p><code>res/layout-large/news_articles.xml:</code></p>
    108 <pre>
    109 &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    110     android:orientation="horizontal"
    111     android:layout_width="fill_parent"
    112     android:layout_height="fill_parent">
    113 
    114     &lt;fragment android:name="com.example.android.fragments.HeadlinesFragment"
    115               android:id="@+id/headlines_fragment"
    116               android:layout_weight="1"
    117               android:layout_width="0dp"
    118               android:layout_height="match_parent" />
    119 
    120     &lt;fragment android:name="com.example.android.fragments.ArticleFragment"
    121               android:id="@+id/article_fragment"
    122               android:layout_weight="2"
    123               android:layout_width="0dp"
    124               android:layout_height="match_parent" />
    125 
    126 &lt;/LinearLayout>
    127 </pre>
    128 
    129 <p class="note"><strong>Tip:</strong> For more information about creating layouts for different
    130 screen sizes, read <a href="{@docRoot}training/multiscreen/screensizes.html">Supporting Different
    131 Screen Sizes</a>.</p>
    132 
    133 <p>Here's how an activity applies this layout:</p>
    134 
    135 <pre>
    136 import android.os.Bundle;
    137 import android.support.v4.app.FragmentActivity;
    138 
    139 public class MainActivity extends FragmentActivity {
    140     &#64;Override
    141     public void onCreate(Bundle savedInstanceState) {
    142         super.onCreate(savedInstanceState);
    143         setContentView(R.layout.news_articles);
    144     }
    145 }
    146 </pre>
    147 
    148 
    149 <p class="note"><strong>Note:</strong> When you add a fragment to an activity layout by defining
    150 the fragment in the layout XML file, you <em>cannot</em> remove the fragment at runtime. If you plan
    151 to swap your fragments in and out during user interaction, you must add the fragment to the activity
    152 when the activity first starts, as shown in the next lesson.</p>
    153 
    154 
    155 
    156