1 page.title= 2 parent.title= 3 parent.link=activities.html 4 @jd:body 5 6 <div id="qv-wrapper"> 7 <div id="qv"> 8 <h2></h2> 9 <ol> 10 <li><a href="#Design"></a></li> 11 <li><a href="#Creating"></a> 12 <ol> 13 <li><a href="#UI"> </a></li> 14 <li><a href="#Adding"></a></li> 15 </ol> 16 </li> 17 <li><a href="#Managing"></a></li> 18 <li><a href="#Transactions"></a></li> 19 <li><a href="#CommunicatingWithActivity"></a> 20 <ol> 21 <li><a href="#EventCallbacks"> </a></li> 22 <li><a href="#ActionBar"></a></li> 23 </ol> 24 </li> 25 <li><a href="#Lifecycle"></a> 26 <ol> 27 <li><a href="#CoordinatingWithActivity"></a></li> 28 </ol> 29 </li> 30 <li><a href="#Example"></a></li> 31 </ol> 32 33 <h2></h2> 34 <ol> 35 <li>{@link android.app.Fragment}</li> 36 <li>{@link android.app.FragmentManager}</li> 37 <li>{@link android.app.FragmentTransaction}</li> 38 </ol> 39 40 <h2></h2> 41 <ol> 42 <li><a href="{@docRoot}training/basics/fragments/index.html">Building a Dynamic UI with Fragments</a></li> 43 <li><a href="{@docRoot}guide/practices/tablets-and-handsets.html">Supporting Tablets 44 and Handsets</a></li> 45 </ol> 46 </div> 47 </div> 48 49 <p>{@link android.app.Fragment} {@link android.app.Activity} 50 1 UI 51 52 53 54 </p> 55 56 <p> 57 58 <a href="{@docRoot}guide/components/activities.html#Lifecycle"></a><em></em> 59 60 61 62 — 63 [<em></em>] 64 </p> 65 66 <p> {@link 67 android.view.ViewGroup} {@code <fragment>} {@link android.view.ViewGroup} 68 69 70 71 UI 72 73 </p> 74 75 <p> 76 77 78 </p> 79 80 81 <h2 id="Design"></h2> 82 83 <p>Android UI Android 3.0API 11 84 UI 85 86 87 88 89 </p> 90 91 <p>1 1 92 — 93 94 1 1 1 95 96 </p> 97 98 <p> 1 1 99 100 101 102 103 104 2 UI 105 106 </p> 107 108 <img src="{@docRoot}images/fundamentals/fragments.png" alt="" /> 109 <p class="img-caption"><strong> 1.</strong> 1 2 UI 110 111 </p> 112 113 <p> 2 <em> A</em> —— 114 <em> A</em> 2 <em> B</em> 115 116 117 1 118 119 </p> 120 121 <p><a href="{@docRoot}guide/practices/tablets-and-handsets.html">Supporting Tablets and Handsets</a> 122 </p> 123 124 125 126 <h2 id="Creating"></h2> 127 128 <div class="figure" style="width:327px"> 129 <img src="{@docRoot}images/fragment_lifecycle.png" alt="" /> 130 <p class="img-caption"><strong> 2.</strong> 131 </p> 132 </div> 133 134 <p>{@link android.app.Fragment} 135 {@link android.app.Fragment} {@link android.app.Activity} 136 {@link android.app.Fragment#onCreate onCreate()}{@link android.app.Fragment#onStart onStart()}{@link android.app.Fragment#onPause onPause()}{@link android.app.Fragment#onStop onStop()} 137 138 Android 139 140 141 </p> 142 143 <p> </p> 144 145 <dl> 146 <dt>{@link android.app.Fragment#onCreate onCreate()}</dt> 147 <dd> 148 149 </dd> 150 <dt>{@link android.app.Fragment#onCreateView onCreateView()}</dt> 151 <dd> 152 UI {@link android.view.View} 153 UI null 154 </dd> 155 <dt>{@link android.app.Activity#onPause onPause()}</dt> 156 <dd> 157 158 159 </dd> 160 </dl> 161 162 <p> 3 163 164 <a href="#Lifecycle"></a> 165 </p> 166 167 168 <p> {@link 169 android.app.Fragment} </p> 170 171 <dl> 172 <dt>{@link android.app.DialogFragment}</dt> 173 <dd> {@link android.app.Activity} 174 175 176 </dd> 177 178 <dt>{@link android.app.ListFragment}</dt> 179 <dd>{@link android.app.ListActivity} {@link 180 android.widget.SimpleCursorAdapter} {@link 181 android.app.ListFragment#onListItemClick(ListView,View,int,long) onListItemClick()} 182 183 </dd> 184 185 <dt>{@link android.preference.PreferenceFragment}</dt> 186 <dd>{@link android.preference.PreferenceActivity} {@link android.preference.Preference} 187 188 </dd> 189 </dl> 190 191 192 <h3 id="UI"> </h3> 193 194 <p> 195 </p> 196 197 <p>{@link 198 android.app.Fragment#onCreateView onCreateView()} Android 199 {@link android.view.View} 200 </p> 201 202 <p class="note"><strong>:</strong> {@link 203 android.app.ListFragment} {@link android.widget.ListView} {@link android.app.Fragment#onCreateView onCreateView()} 204 </p> 205 206 <p>{@link 207 android.app.Fragment#onCreateView onCreateView()} XML <a href="{@docRoot}guide/topics/resources/layout-resource.html"> </a>{@link android.app.Fragment#onCreateView onCreateView()} {@link android.view.LayoutInflater} 208 209 </p> 210 211 <p> {@link android.app.Fragment} {@code example_fragment.xml} 212 </p> 213 214 <pre> 215 public static class ExampleFragment extends Fragment { 216 @Override 217 public View onCreateView(LayoutInflater inflater, ViewGroup container, 218 Bundle savedInstanceState) { 219 // Inflate the layout for this fragment 220 return inflater.inflate(R.layout.example_fragment, container, false); 221 } 222 } 223 </pre> 224 225 <div class="sidebox-wrapper"> 226 <div class="sidebox"> 227 <h3></h3> 228 <p>{@code R.layout.example_fragment} {@code example_fragment.xml} 229 XML <a href="{@docRoot}guide/topics/ui/index.html"> </a> 230 231 </p> 232 </div> 233 </div> 234 235 <p>{@link android.app.Fragment#onCreateView 236 onCreateView()} {@code container} {@link android.view.ViewGroup} 237 238 {@code savedInstanceState} {@link android.os.Bundle} <a href="#Lifecycle"></a> 239 240 241 </p> 242 243 <p>{@link android.view.LayoutInflater#inflate(int,ViewGroup,boolean) inflate()} 3 244 </p> 245 <ul> 246 <li> ID</li> 247 <li> {@link android.view.ViewGroup} {@code 248 container} 249 </li> 250 <li> {@link 251 android.view.ViewGroup}2 {@code 252 container} false true 253 —</li> 254 </ul> 255 256 <p> 257 </p> 258 259 260 261 <h3 id="Adding"></h3> 262 263 <p> UI 264 2 265 </p> 266 267 <ul> 268 <li><b> </b> 269 <p> 270 2 271 </p> 272 <pre> 273 <?xml version="1.0" encoding="utf-8"?> 274 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 275 android:orientation="horizontal" 276 android:layout_width="match_parent" 277 android:layout_height="match_parent"> 278 <fragment android:name="com.example.news.ArticleListFragment" 279 android:id="@+id/list" 280 android:layout_weight="1" 281 android:layout_width="0dp" 282 android:layout_height="match_parent" /> 283 <fragment android:name="com.example.news.ArticleReaderFragment" 284 android:id="@+id/viewer" 285 android:layout_weight="2" 286 android:layout_width="0dp" 287 android:layout_height="match_parent" /> 288 </LinearLayout> 289 </pre> 290 <p>{@code <fragment>} {@code android:name} {@link 291 android.app.Fragment} </p> 292 293 <p> {@link android.app.Fragment#onCreateView onCreateView()} 294 295 {@link android.view.View} {@code <fragment>} 296 </p> 297 298 <div class="note"> 299 <p><strong>:</strong> 300 301 ID 3 302 </p> 303 <ul> 304 <li>{@code android:id} ID </li> 305 <li>{@code android:tag} </li> 306 <li> ID 307 </li> 308 </ul> 309 </div> 310 </li> 311 312 <li><b> {@link android.view.ViewGroup} </b> 313 <p> {@link 314 android.view.ViewGroup} 315 </p> 316 <p>{@link android.app.FragmentTransaction} API 317 {@link android.app.FragmentTransaction} {@link android.app.Activity} 318 </p> 319 320 <pre> 321 FragmentManager fragmentManager = {@link android.app.Activity#getFragmentManager()} 322 FragmentTransaction fragmentTransaction = fragmentManager.{@link android.app.FragmentManager#beginTransaction()}; 323 </pre> 324 325 <p>{@link 326 android.app.FragmentTransaction#add(int,Fragment) add()} 327 </p> 328 329 <pre> 330 ExampleFragment fragment = new ExampleFragment(); 331 fragmentTransaction.add(R.id.fragment_container, fragment); 332 fragmentTransaction.commit(); 333 </pre> 334 335 <p>{@link android.app.FragmentTransaction#add(int,Fragment) add()} {@link android.view.ViewGroup} ID 2 336 337 </p> 338 <p>{@link android.app.FragmentTransaction} {@link android.app.FragmentTransaction#commit} 339 340 </p> 341 </li> 342 </ul> 343 344 345 <h4 id="AddingWithoutUI">UI </h4> 346 347 <p>UI UI 348 349 </p> 350 351 <p>UI {@link 352 android.app.FragmentTransaction#add(Fragment,String)} ID 353 {@link 354 android.app.Fragment#onCreateView onCreateView()} 355 </p> 356 357 <p> UI UI UI — 358 — 359 {@link android.app.FragmentManager#findFragmentByTag 360 findFragmentByTag()} 361 </p> 362 363 <p> UI SDK Android SDK {@code 364 FragmentRetainInstance.java} <code><sdk_root>/APIDemos/app/src/main/java/com/example/android/apis/app/FragmentRetainInstance.java</code> 365 366 </p> 367 368 369 370 <h2 id="Managing"></h2> 371 372 <p>{@link android.app.FragmentManager} {@link android.app.Activity#getFragmentManager()} 373 </p> 374 375 <p>{@link android.app.FragmentManager} </p> 376 377 <ul> 378 <li>{@link 379 android.app.FragmentManager#findFragmentById findFragmentById()} UI {@link android.app.FragmentManager#findFragmentByTag 380 findFragmentByTag()}UI 381 </li> 382 <li>{@link 383 android.app.FragmentManager#popBackStack()} [<em></em>] </li> 384 <li>{@link 385 android.app.FragmentManager#addOnBackStackChangedListener addOnBackStackChangedListener()} </li> 386 </ul> 387 388 <p>{@link 389 android.app.FragmentManager} </p> 390 391 <p>{@link android.app.FragmentManager} {@link android.app.FragmentTransaction} 392 393 </p> 394 395 396 <h2 id="Transactions"></h2> 397 398 <p> 399 1 {@link 400 android.app.FragmentTransaction} API 401 402 403 </p> 404 405 <p>{@link android.app.FragmentTransaction} {@link 406 android.app.FragmentManager} </p> 407 408 <pre> 409 FragmentManager fragmentManager = {@link android.app.Activity#getFragmentManager()}; 410 FragmentTransaction fragmentTransaction = fragmentManager.{@link android.app.FragmentManager#beginTransaction()}; 411 </pre> 412 413 <p>{@link 414 android.app.FragmentTransaction#add add()}{@link android.app.FragmentTransaction#remove remove()}{@link android.app.FragmentTransaction#replace replace()} 415 416 {@link android.app.FragmentTransaction#commit()} 417 </p> 418 </dl> 419 420 <p>{@link 421 android.app.FragmentTransaction#commit()} {@link 422 android.app.FragmentTransaction#addToBackStack addToBackStack()} 423 [<em></em>] 424 </p> 425 426 <p> 427 </p> 428 429 <pre> 430 // Create new fragment and transaction 431 Fragment newFragment = new ExampleFragment(); 432 FragmentTransaction transaction = getFragmentManager().beginTransaction(); 433 434 // Replace whatever is in the fragment_container view with this fragment, 435 // and add the transaction to the back stack 436 transaction.replace(R.id.fragment_container, newFragment); 437 transaction.addToBackStack(null); 438 439 // Commit the transaction 440 transaction.commit(); 441 </pre> 442 443 <p>{@code R.id.fragment_container} ID {@code newFragment} {@link 444 android.app.FragmentTransaction#addToBackStack addToBackStack()} [<em></em>] 445 446 447 </p> 448 449 <p>{@link 450 android.app.FragmentTransaction#add add()} {@link android.app.FragmentTransaction#remove 451 remove()} {@link 452 android.app.FragmentTransaction#addToBackStack addToBackStack()} {@link android.app.FragmentTransaction#commit commit()} 1 [<em></em>] 453 454 </p> 455 456 <p>{@link android.app.FragmentTransaction} 457 </p> 458 <ul> 459 <li>{@link android.app.FragmentTransaction#commit()} </li> 460 <li> 461 </li> 462 </ul> 463 464 <p> {@link android.app.FragmentTransaction#addToBackStack(String) 465 addToBackStack()} 466 {@link android.app.FragmentTransaction#addToBackStack(String) addToBackStack()} <em></em> 467 468 469 </p> 470 471 <p class="note"><strong>:</strong> {@link android.app.FragmentTransaction#setTransition setTransition()} 472 473 </p> 474 475 <p>{@link android.app.FragmentTransaction#commit()} 476 UI 477 UI {@link 478 android.app.FragmentManager#executePendingTransactions()} {@link android.app.FragmentTransaction#commit()} 479 480 </p> 481 482 <p class="caution"><strong>:</strong> {@link 483 android.app.FragmentTransaction#commit commit()} <a href="{@docRoot}guide/components/activities.html#SavingActivityState"></a> 484 485 486 {@link 487 android.app.FragmentTransaction#commitAllowingStateLoss()} </p> 488 489 490 491 492 <h2 id="CommunicatingWithActivity"></h2> 493 494 <p>{@link android.app.Fragment} {@link android.app.Activity} 495 496 </p> 497 498 <p> {@link 499 android.app.Fragment#getActivity()} {@link android.app.Activity} 500 </p> 501 502 <pre> 503 View listView = {@link android.app.Fragment#getActivity()}.{@link android.app.Activity#findViewById findViewById}(R.id.list); 504 </pre> 505 506 <p> {@link 507 android.app.FragmentManager#findFragmentById findFragmentById()} {@link 508 android.app.FragmentManager#findFragmentByTag findFragmentByTag()} {@link android.app.FragmentManager} {@link android.app.Fragment} 509 </p> 510 511 <pre> 512 ExampleFragment fragment = (ExampleFragment) getFragmentManager().findFragmentById(R.id.example_fragment); 513 </pre> 514 515 516 <h3 id="EventCallbacks"> </h3> 517 518 <p> 519 520 521 </p> 522 523 <p> A B 2 B A — 524 — 525 {@code OnArticleSelectedListener} A 526 </p> 527 528 <pre> 529 public static class FragmentA extends ListFragment { 530 ... 531 // Container Activity must implement this interface 532 public interface OnArticleSelectedListener { 533 public void onArticleSelected(Uri articleUri); 534 } 535 ... 536 } 537 </pre> 538 539 <p> {@code OnArticleSelectedListener} {@code onArticleSelected()} B A A {@link 540 android.app.Fragment#onAttach onAttach()} {@link android.app.Fragment#onAttach 541 onAttach()} {@link android.app.Activity} {@code OnArticleSelectedListener} 542 543 544 545 546 </p> 547 548 <pre> 549 public static class FragmentA extends ListFragment { 550 OnArticleSelectedListener mListener; 551 ... 552 @Override 553 public void onAttach(Activity activity) { 554 super.onAttach(activity); 555 try { 556 mListener = (OnArticleSelectedListener) activity; 557 } catch (ClassCastException e) { 558 throw new ClassCastException(activity.toString() + " must implement OnArticleSelectedListener"); 559 } 560 } 561 ... 562 } 563 </pre> 564 565 <p> {@link java.lang.ClassCastException} {@code mListener} {@code OnArticleSelectedListener} A {@code OnArticleSelectedListener} 566 567 568 569 A {@link android.app.ListFragment} {@link android.app.ListFragment#onListItemClick 570 onListItemClick()} {@code onArticleSelected()} 571 572 573 </p> 574 575 <pre> 576 public static class FragmentA extends ListFragment { 577 OnArticleSelectedListener mListener; 578 ... 579 @Override 580 public void onListItemClick(ListView l, View v, int position, long id) { 581 // Append the clicked item's row ID with the content provider Uri 582 Uri noteUri = ContentUris.{@link android.content.ContentUris#withAppendedId withAppendedId}(ArticleColumns.CONTENT_URI, id); 583 // Send the event and Uri to the host activity 584 mListener.onArticleSelected(noteUri); 585 } 586 ... 587 } 588 </pre> 589 590 <p>{@link 591 android.app.ListFragment#onListItemClick onListItemClick()} {@code id} ID {@link 592 android.content.ContentProvider} 593 </p> 594 595 <p><!--To see a complete implementation of this kind of callback interface, see the <a 596 href="{@docRoot}resources/samples/NotePad/index.html">NotePad sample</a>. --> <a href="{@docRoot}guide/topics/providers/content-providers.html"> </a> 597 </p> 598 599 600 601 <h3 id="ActionBar"></h3> 602 603 <p>{@link android.app.Fragment#onCreateOptionsMenu(Menu,MenuInflater) onCreateOptionsMenu()} <a href="{@docRoot}guide/topics/ui/menus.html#options-menu"> </a><a href="{@docRoot}guide/topics/ui/actionbar.html"> </a> 604 {@link 605 android.app.Fragment#onCreate(Bundle) onCreate()} {@link 606 android.app.Fragment#setHasOptionsMenu(boolean) setHasOptionsMenu()} {@link android.app.Fragment#onCreateOptionsMenu onCreateOptionsMenu()} 607 608 609 </p> 610 611 <p> 612 {@link 613 android.app.Fragment#onOptionsItemSelected(MenuItem) onOptionsItemSelected()} 614 </p> 615 616 <p>{@link 617 android.app.Fragment#registerForContextMenu(View) registerForContextMenu()} {@link 618 android.app.Fragment#onCreateContextMenu(ContextMenu,View,ContextMenu.ContextMenuInfo) 619 onCreateContextMenu()} 620 {@link 621 android.app.Fragment#onContextItemSelected(MenuItem) onContextItemSelected()} </p> 622 623 <p class="note"><strong>:</strong> on-item-selected 624 625 on-item-selected 626 627 </p> 628 629 <p> <a href="{@docRoot}guide/topics/ui/menus.html"></a><a href="{@docRoot}guide/topics/ui/actionbar.html"></a></p> 630 631 632 633 634 <h2 id="Lifecycle"></h2> 635 636 <div class="figure" style="width:350px"> 637 <img src="{@docRoot}images/activity_fragment_lifecycle.png" alt="" /> 638 <p class="img-caption"><strong> 3.</strong> 639 </p> 640 </div> 641 642 <p> 3 643 </p> 644 645 <dl> 646 <dt><i></i></dt> 647 <dd></dd> 648 649 <dt><i></i></dt> 650 <dd> 651 652 </dd> 653 654 <dt><i></i></dt> 655 <dd> 656 657 658 </dd> 659 </dl> 660 661 <p> {@link 662 android.os.Bundle} 663 {@link 664 android.app.Fragment#onSaveInstanceState onSaveInstanceState()} {@link android.app.Fragment#onCreate onCreate()}{@link 665 android.app.Fragment#onCreateView onCreateView()}{@link 666 android.app.Fragment#onActivityCreated onActivityCreated()} 667 <a href="{@docRoot}guide/components/activities.html#SavingActivityState">Activities</a> 668 669 </p> 670 671 <p> 672 <a href="{@docRoot}guide/components/tasks-and-back-stack.html"></a> [<em></em>] {@link 673 android.app.FragmentTransaction#addToBackStack(String) addToBackStack()} 674 675 676 677 678 </p> 679 680 <p> 681 <a href="{@docRoot}guide/components/activities.html#Lifecycle"></a> 682 1 683 </p> 684 685 <p class="caution"><strong>:</strong> {@link android.app.Fragment} {@link android.content.Context} {@link android.app.Fragment#getActivity()} {@link android.app.Fragment#getActivity()} 686 687 688 {@link android.app.Fragment#getActivity()} null 689 </p> 690 691 692 <h3 id="CoordinatingWithActivity"></h3> 693 694 <p> 695 696 {@link android.app.Activity#onPause} {@link android.app.Fragment#onPause} 697 </p> 698 699 <p> UI 700 701 </p> 702 703 <dl> 704 <dt>{@link android.app.Fragment#onAttach onAttach()}</dt> 705 <dd> {@link 706 android.app.Activity} </dd> 707 <dt>{@link android.app.Fragment#onCreateView onCreateView()}</dt> 708 <dd></dd> 709 <dt>{@link android.app.Fragment#onActivityCreated onActivityCreated()}</dt> 710 <dd> {@link android.app.Activity#onCreate 711 onCreate()} </dd> 712 <dt>{@link android.app.Fragment#onDestroyView onDestroyView()}</dt> 713 <dd></dd> 714 <dt>{@link android.app.Fragment#onDetach onDetach()}</dt> 715 <dd></dd> 716 </dl> 717 718 <p> 3 719 720 {@link 721 android.app.Activity#onCreate onCreate()} {@link android.app.Fragment#onActivityCreated onActivityCreated()} 722 </p> 723 724 <p> 725 726 </p> 727 728 <p> 729 </p> 730 731 732 733 734 <h2 id="Example"></h2> 735 736 <p>2 2 737 738 739 740 </p> 741 742 <p class="note"><strong>:</strong> <a href="{@docRoot}resources/samples/ApiDemos/src/com/example/android/apis/app/FragmentLayout.html">{@code 743 FragmentLayout.java}</a> 744 </p> 745 746 <p> {@link 747 android.app.Activity#onCreate onCreate()} </p> 748 749 {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentLayout.java main} 750 751 <p> {@code fragment_layout.xml} </p> 752 753 {@sample development/samples/ApiDemos/res/layout-land/fragment_layout.xml layout} 754 755 <p> {@code TitlesFragment}{@link android.widget.FrameLayout} 756 757 758 {@link android.widget.FrameLayout} 759 </p> 760 761 <p> 762 {@code res/layout-land/fragment_layout.xml} 763 </p> 764 765 <p> {@code res/layout/fragment_layout.xml} 766 </p> 767 768 {@sample development/samples/ApiDemos/res/layout/fragment_layout.xml layout} 769 770 <p>{@code TitlesFragment} 771 2 772 773 </p> 774 775 <p>{@code 776 TitlesFragment} {@link 777 android.app.ListFragment} </p> 778 779 <p> 2 2 {@link 780 android.widget.FrameLayout} 781 782 </p> 783 784 {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentLayout.java titles} 785 786 <p>2 {@code DetailsFragment} {@code TitlesFragment} 787 </p> 788 789 {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentLayout.java details} 790 791 <p>{@code TitlesFragment} {@code R.id.details} {@code DetailsFragment} <em></em> {@code DetailsActivity} 792 793 794 </p> 795 796 <p> {@code DetailsActivity} {@code DetailsFragment} 797 </p> 798 799 {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentLayout.java 800 details_activity} 801 802 <p> {@code DetailsFragment} {@code TitlesFragment} {@code DetailsActivity} 803 804 805 </p> 806 807 808 <p> <a href="{@docRoot}resources/samples/ApiDemos/src/com/example/android/apis/app/index.html#Fragment">ApiDemos</a><a href="{@docRoot}resources/samples/get.html">SDK </a> API 809 810 </p> 811 812 813