Home | History | Annotate | Download | only in fragments
      1 page.title=
      2 trainingnavtop=true
      3 page.type=
      4 @jd:body
      5 
      6     <div id="tb-wrapper">
      7       <div id="tb"> 
      8         <h2></h2>
      9     <ol>
     10       <li><a href="#AddAtRuntime"> Activity  Fragment</a></li>
     11       <li><a href="#Replace"> Fragment  Fragment </a></li>
     12     </ol>
     13 
     14         <h2></h2>
     15         <ul>
     16           <li><a href="{@docRoot}guide/components/fragments.html"><code>fragment</code></a></li>
     17           <li><a href="{@docRoot}guide/practices/tablets-and-handsets.html"></a></li>
     18         </ul>
     19 
     20     <h2></h2>
     21 
     22     <div class="download-box">
     23      <a href="http://developer.android.com/shareables/training/FragmentBasics.zip" class="button"></a>
     24      <p class="filename">FragmentBasics.zip</p>
     25     </div>
     26 
     27       </div>
     28     </div>
     29 
     30 
     31     <p> Fragment </p>
     32 
     33     <p> Fragment  Fragment</p>
     34 
     35     <img src="{@docRoot}images/training/basics/fragments-screen-mock.png" alt="" />
     36     <p class="img-caption"><strong> 1</strong>  Fragment Activity  Fragment  Fragment Fragment</p>
     37 
     38     <p> <code><a href="{@docRoot}reference/android/support/v4/app/FragmentManager.html">FragmentManager</a></code>  Activity  Fragment</p>
     39 
     40 
     41 
     42     <h2 id="AddAtRuntime"> Activity  Fragment</h2> 
     43 
     44     <p> Activity  Fragment<a href="creating.html"></a> <code>&lt;fragment&gt;</code>  Activity  Fragment Activity  Fragment</p>
     45 
     46     <p> Fragment  <code><a href="{@docRoot}reference/android/support/v4/app/FragmentManager.html">FragmentManager</a></code>  <code><a href="{@docRoot}reference/android/support/v4/app/FragmentTransaction.html">FragmentTransaction</a></code> Fragment  API</p>
     47 
     48     <p> Activity  Fragment  Activity  <code><a href="{@docRoot}reference/android/app/Activity.html#onCreate(android.os.Bundle)">onCreate()</a></code>  Activity  Fragment(s)</p>
     49 
     50     <p> Fragment Fragment  Fragment  <code><a href="{@docRoot}reference/android/view/View.html">View</a></code>  Fragment </p>
     51 
     52     <p><a href="creating.html"></a> Fragment Fragment  Fragment Activity  Fragment  <code><a href="{@docRoot}reference/android/widget/FrameLayout.html">FrameLayout</a></code></p>
     53 
     54     <p><em></em> <code>large</code> large Fragment<em></em></p>
     55 
     56     <p><code>res/layout/news_articles.xml:</code></p>
     57     <pre>
     58     &lt;FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
     59         android:id="@+id/fragment_container"
     60         android:layout_width="match_parent"
     61         android:layout_height="match_parent" /&gt;
     62     </pre>
     63 
     64     <p> Activity  Support Library API  <code><a href="{@docRoot}reference/android/support/v4/app/FragmentActivity.html#getSupportFragmentManager()">getSupportFragmentManager()</a></code>  <code><a href="{@docRoot}reference/android/support/v4/app/FragmentManager.html">FragmentManager</a></code> <code><a href="{@docRoot}reference/android/support/v4/app/FragmentManager.html#beginTransaction()">beginTransaction()</a></code>  <code><a href="{@docRoot}reference/android/support/v4/app/FragmentTransaction.html">FragmentTransaction</a></code> <code><a href="{@docRoot}reference/android/support/v4/app/FragmentTransaction.html#add(android.support.v4.app.Fragment, java.lang.String)">add()</a></code>  Fragment</p>
     65 
     66     <p> <code><a href="{@docRoot}reference/android/support/v4/app/FragmentTransaction.html">FragmentTransaction</a></code>  Activity  Fragment  <code><a href="{@docRoot}reference/android/support/v4/app/FragmentTransaction.html#commit()">commit()</a></code></p>
     67 
     68     <p> Fragment </p>
     69 
     70     <pre>
     71     import android.os.Bundle;
     72     import android.support.v4.app.FragmentActivity;
     73 
     74     public class MainActivity extends FragmentActivity {
     75         &amp;Override
     76         public void onCreate(Bundle savedInstanceState) {
     77             super.onCreate(savedInstanceState);
     78             setContentView(R.layout.news_articles);
     79 
     80             //  Activity 
     81             // fragment_container FrameLayout
     82             if (findViewById(R.id.fragment_container) != null) {
     83 
     84                 // 
     85                 // 
     86                 //  Fragment 
     87                 if (savedInstanceState != null) {
     88                     return;
     89                 }
     90 
     91                 //  Activity  Fragment 
     92                 HeadlinesFragment firstFragment = new HeadlinesFragment();
     93 
     94                 //  Activity  Intent 
     95                 //  Intent  extras  Fragment 
     96                 firstFragment.setArguments(getIntent().getExtras());
     97 
     98                 //  Fragment fragment_containerFrameLayout 
     99                 getSupportFragmentManager().beginTransaction()
    100                         .add(R.id.fragment_container, firstFragment).commit();
    101             }
    102         }
    103     }
    104     </pre>
    105 
    106     <p> Fragment  <code><a href="{@docRoot}reference/android/widget/FrameLayout.html">FrameLayout</a></code>  Activity  <code>&lt;fragment&gt;</code>  Activity  Fragment </p>
    107 
    108 
    109 
    110     <h2 id="Replace"> Fragment  Fragment </h2>
    111 
    112     <p> Fragment  Fragment  <code><a href="{@docRoot}reference/android/support/v4/app/FragmentTransaction.html#replace(int, android.support.v4.app.Fragment)">replace()</a></code>  <code><a href="{@docRoot}reference/android/support/v4/app/FragmentTransaction.html#add(android.support.v4.app.Fragment, java.lang.String)">add()</a></code></p>
    113 
    114     <p> Fragment  Fragment  Fragment  <code><a href="{@docRoot}reference/android/support/v4/app/FragmentTransaction.html#addToBackStack(java.lang.String)">addToBackStack()</a></code> <code><a href="{@docRoot}reference/android/support/v4/app/FragmentTransaction.html">FragmentTransaction</a></code></p>
    115 
    116     <p class="note"><strong></strong> Fragment  Fragment  Fragment Fragment <em></em></p>
    117 
    118     <p> Fragment </p>
    119 
    120     <pre>
    121     //  Fragment 
    122     ArticleFragment newFragment = new ArticleFragment();
    123     Bundle args = new Bundle();
    124     args.putInt(ArticleFragment.ARG_POSITION, position);
    125     newFragment.setArguments(args);
    126 
    127     FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    128 
    129     //  fragment_container <code>View</code>  Fragment 
    130     // 
    131     transaction.replace(R.id.fragment_container, newFragment);
    132     transaction.addToBackStack(null);
    133 
    134     // 
    135     transaction.commit();
    136     </pre>
    137 
    138     <p><code><a href="{@docRoot}reference/android/support/v4/app/FragmentTransaction.html#addToBackStack(java.lang.String)">addToBackStack()</a></code>   <code><a href="{@docRoot}reference/android/support/v4/app/FragmentManager.BackStackEntry.html">FragmentManager.BackStackEntry</a></code> API  Fragment </p>
    139 
    140