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