1 page.title= 2 page.tags=, 3 @jd:body 4 5 <div id="qv-wrapper"> 6 <div id="qv"> 7 <h2> </h2> 8 <ol> 9 <li><a href="#Creating"> </a> 10 <ol> 11 <li><a href="#UI"> </a></li> 12 <li><a href="#Declaring"> </a></li> 13 </ol> 14 </li> 15 <li><a href="#StartingAnActivity"> </a> 16 <ol> 17 <li><a href="#StartingAnActivityForResult"> </a></li> 18 </ol> 19 </li> 20 <li><a href="#ShuttingDown"> </a></li> 21 <li><a href="#Lifecycle"> </a> 22 <ol> 23 <li><a href="#ImplementingLifecycleCallbacks"> </a></li> 24 <li><a href="#SavingActivityState"> </a></li> 25 <li><a href="#ConfigurationChanges"> </a></li> 26 <li><a href="#CoordinatingActivities"> </a></li> 27 </ol> 28 </li> 29 </ol> 30 31 <h2> </h2> 32 <ol> 33 <li>{@link android.app.Activity}</li> 34 </ol> 35 36 <h2>. :</h2> 37 <ol> 38 <li><a href="{@docRoot}guide/components/tasks-and-back-stack.html"> 39 </a></li> 40 </ol> 41 42 </div> 43 </div> 44 45 46 47 <p>{@link android.app.Activity} , , 48 - , , , 49 . . 50 , , 51 .</p> 52 53 <p> , , 54 . , 55 . , 56 . , 57 , , 58 ( ). 59 . , 60 <em></em>, 61 ( ), . ( 62 <a href="{@docRoot}guide/components/tasks-and-back-stack.html"> 63 </a>.)</p> 64 65 <p> , 66 . 67 , 68 — , , ; 69 , 70 . , 71 , , . 72 . 73 .</p> 74 75 <p> , 76 , , 77 .</p> 78 79 80 81 <h2 id="Creating"> </h2> 82 83 <p> , {@link android.app.Activity} ( 84 ). , 85 , 86 , , . 87 :</p> 88 89 <dl> 90 <dt>{@link android.app.Activity#onCreate onCreate()}</dt> 91 <dd> , 92 . 93 . 94 {@link android.app.Activity#setContentView 95 setContentView()} .</dd> 96 <dt>{@link android.app.Activity#onPause onPause()}</dt> 97 <dd> 98 ( , ). 99 , ( 100 ).</dd> 101 </dl> 102 103 <p> , , 104 , , 105 . , 106 <a href="#Lifecycle"> </a>.</p> 107 108 109 110 <h3 id="UI"> </h3> 111 112 <p> —, 113 {@link android.view.View}. 114 . , 115 , .</p> 116 117 <p> Android , 118 . ( ) , , 119 , , . , {@link 120 android.view.ViewGroup}, , 121 , . {@link android.view.View} 122 {@link android.view.ViewGroup} ( ), 123 , .</p> 124 125 <p> XML- , 126 . 127 , . 128 , {@link android.app.Activity#setContentView(int) setContentView()}, 129 . {@link android.view.View} 130 . {@link 131 android.view.View} {@link android.view.ViewGroup}, , 132 {@link android.view.ViewGroup} {@link android.app.Activity#setContentView(View) 133 setContentView()}.</p> 134 135 <p> . <a href="{@docRoot}guide/topics/ui/index.html"> </a>.</p> 136 137 138 139 <h3 id="Declaring"> </h3> 140 141 <p> , 142 . <a href="{@docRoot}guide/topics/manifest/activity-element.html">{@code <activity>}</a> 143 144 <a href="{@docRoot}guide/topics/manifest/application-element.html">{@code <application>}</a>. :</p> 145 146 <pre> 147 <manifest ... > 148 <application ... > 149 <activity android:name=".ExampleActivity" /> 150 ... 151 </application ... > 152 ... 153 </manifest > 154 </pre> 155 156 <p> , , 157 , , . 158 <a href="{@docRoot}guide/topics/manifest/activity-element.html#nm">{@code android:name}</a> 159 — . 160 , 161 , , ( <a href="http://android-developers.blogspot.com/2011/06/things-that-cannot-change.html"> 162 , </a> ).</p> 163 164 <p> 165 . <a href="{@docRoot}guide/topics/manifest/activity-element.html">{@code <activity>}</a>.</p> 166 167 168 <h4> </h4> 169 170 <p> <a href="{@docRoot}guide/topics/manifest/activity-element.html">{@code 171 <activity>}</a> — <a href="{@docRoot}guide/topics/manifest/intent-filter-element.html">{@code 172 <intent-filter>}</a>— , 173 .</p> 174 175 <p> Android SDK , 176 , , . 177 , . 178 .</p> 179 180 <pre> 181 <activity android:name=".ExampleActivity" android:icon="@drawable/app_icon"> 182 <intent-filter> 183 <action android:name="android.intent.action.MAIN" /> 184 <category android:name="android.intent.category.LAUNCHER" /> 185 </intent-filter> 186 </activity> 187 </pre> 188 189 <p> <a href="{@docRoot}guide/topics/manifest/action-element.html">{@code 190 <action>}</a> , . <a href="{@docRoot}guide/topics/manifest/category-element.html">{@code 191 <category>}</a>, 192 ( ).</p> 193 194 <p> 195 , . 196 , , . , 197 , . 198 ( ).</p> 199 200 <p>, , , 201 ( ), 202 . , , <a href="{@docRoot}guide/topics/manifest/intent-filter-element.html">{@code 203 <intent-filter>}</a>, 204 <a href="{@docRoot}guide/topics/manifest/action-element.html">{@code 205 <action>}</a> <a href="{@docRoot}guide/topics/manifest/category-element.html">{@code 206 <category>}</a> <a href="{@docRoot}guide/topics/manifest/data-element.html">{@code 207 <data>}</a> ( ). , 208 .</p> 209 210 <p> , , <a href="{@docRoot}guide/components/intents-filters.html"> </a>. 211 </p> 212 213 214 215 <h2 id="StartingAnActivity"> </h2> 216 217 <p> {@link android.app.Activity#startActivity 218 startActivity()}, {@link android.content.Intent}, 219 . , 220 , ( 221 , 222 ). , 223 .</p> 224 225 <p> . 226 , 227 . {@code 228 SignInActivity}.</p> 229 230 <pre> 231 Intent intent = new Intent(this, SignInActivity.class); 232 startActivity(intent); 233 </pre> 234 235 <p> , , , 236 , . 237 , 238 , , . 239 — , , 240 241 . 242 , , , . , 243 , 244 :</p> 245 246 <pre> 247 Intent intent = new Intent(Intent.ACTION_SEND); 248 intent.putExtra(Intent.EXTRA_EMAIL, recipientArray); 249 startActivity(intent); 250 </pre> 251 252 <p> {@link android.content.Intent#EXTRA_EMAIL}, , 253 . 254 , 255 . , , 256 , .</p> 257 258 259 260 261 <h3 id="StartingAnActivityForResult"> </h3> 262 263 <p> . 264 {@link android.app.Activity#startActivityForResult 265 startActivityForResult()} ( {@link android.app.Activity#startActivity 266 startActivity()}). 267 , 268 {@link android.app.Activity#onActivityResult onActivityResult()}. {@link 269 android.content.Intent} 270 {@link android.app.Activity#onActivityResult onActivityResult()}.</p> 271 272 <p> , , 273 . 274 .</p> 275 276 <pre> 277 private void pickContact() { 278 // Create an intent to "pick" a contact, as defined by the content provider URI 279 Intent intent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI); 280 startActivityForResult(intent, PICK_CONTACT_REQUEST); 281 } 282 283 @Override 284 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 285 // If the request went well (OK) and the request was PICK_CONTACT_REQUEST 286 if (resultCode == Activity.RESULT_OK && requestCode == PICK_CONTACT_REQUEST) { 287 // Perform a query to the contact's content provider for the contact's name 288 Cursor cursor = getContentResolver().query(data.getData(), 289 new String[] {Contacts.DISPLAY_NAME}, null, null, null); 290 if (cursor.moveToFirst()) { // True if the cursor is not empty 291 int columnIndex = cursor.getColumnIndex(Contacts.DISPLAY_NAME); 292 String name = cursor.getString(columnIndex); 293 // Do something with the selected contact's name... 294 } 295 } 296 } 297 </pre> 298 299 <p> , {@link 300 android.app.Activity#onActivityResult onActivityResult()} 301 . , , , 302 {@code resultCode} {@link android.app.Activity#RESULT_OK}; , , 303 , {@code requestCode} 304 , {@link android.app.Activity#startActivityForResult 305 startActivityForResult()}. , 306 {@link android.content.Intent} ( {@code data}).</p> 307 308 <p> {@link 309 android.content.ContentResolver} , 310 {@link android.database.Cursor}, . 311 <a href="{@docRoot}guide/topics/providers/content-providers.html"> </a>.</p> 312 313 <p> . <a href="{@docRoot}guide/components/intents-filters.html"> 314 </a>.</p> 315 316 317 <h2 id="ShuttingDown"> </h2> 318 319 <p> {@link android.app.Activity#finish 320 finish()}. , , 321 {@link android.app.Activity#finishActivity finishActivity()}.</p> 322 323 <p class="note"><strong>.</strong> 324 . , , 325 Android , 326 . 327 . , , 328 .</p> 329 330 331 <h2 id="Lifecycle"> </h2> 332 333 <p> 334 335 . 336 , .</p> 337 338 <p> :</p> 339 340 <dl> 341 <dt><i></i></dt> 342 <dd> . ( 343 .)</dd> 344 345 <dt><i></i></dt> 346 <dd> , , - . 347 , 348 . ( {@link android.app.Activity} 349 - , , 350 ), .</dd> 351 352 <dt><i></i></dt> 353 <dd> ( 354 ). - ( {@link android.app.Activity} 355 - , , <em></em> 356 ). , 357 .</dd> 358 </dl> 359 360 <p> , 361 ( {@link android.app.Activity#finish finish()}) 362 . ( ) 363 .</p> 364 365 366 367 <h3 id="ImplementingLifecycleCallbacks"> </h3> 368 369 <p> , 370 . , 371 . 372 .</p> 373 374 375 <pre> 376 public class ExampleActivity extends Activity { 377 @Override 378 public void {@link android.app.Activity#onCreate onCreate}(Bundle savedInstanceState) { 379 super.onCreate(savedInstanceState); 380 // The activity is being created. 381 } 382 @Override 383 protected void {@link android.app.Activity#onStart onStart()} { 384 super.onStart(); 385 // The activity is about to become visible. 386 } 387 @Override 388 protected void {@link android.app.Activity#onResume onResume()} { 389 super.onResume(); 390 // The activity has become visible (it is now "resumed"). 391 } 392 @Override 393 protected void {@link android.app.Activity#onPause onPause()} { 394 super.onPause(); 395 // Another activity is taking focus (this activity is about to be "paused"). 396 } 397 @Override 398 protected void {@link android.app.Activity#onStop onStop()} { 399 super.onStop(); 400 // The activity is no longer visible (it is now "stopped") 401 } 402 @Override 403 protected void {@link android.app.Activity#onDestroy onDestroy()} { 404 super.onDestroy(); 405 // The activity is about to be destroyed. 406 } 407 } 408 </pre> 409 410 <p class="note"><strong>.</strong> 411 , - , .</p> 412 413 <p> . 414 : </p> 415 416 <ul> 417 <li><b> </b> {@link 418 android.app.Activity#onCreate onCreate()} {@link 419 android.app.Activity#onDestroy}. 420 (, ) {@link android.app.Activity#onCreate onCreate()}, 421 {@link android.app.Activity#onDestroy} . , 422 , , , 423 {@link android.app.Activity#onCreate onCreate()}, {@link 424 android.app.Activity#onDestroy}.</li> 425 426 <li><p><b> </b> {@link 427 android.app.Activity#onStart onStart()} {@link 428 android.app.Activity#onStop onStop()}. 429 , . , {@link android.app.Activity#onStop onStop()} 430 , , . 431 , . , 432 {@link android.content.BroadcastReceiver} {@link 433 android.app.Activity#onStart onStart()} , , 434 {@link android.app.Activity#onStop onStop()}, 435 . {@link android.app.Activity#onStart onStart()} {@link 436 android.app.Activity#onStop onStop()}, 437 , .</p></li> 438 439 <li><p><b> , </b>, {@link 440 android.app.Activity#onResume onResume()} {@link android.app.Activity#onPause 441 onPause()}. 442 . — , 443 {@link android.app.Activity#onPause onPause()} 444 . , 445 , .</p></li> 446 </ul> 447 448 <p> 1 , . 449 , 450 . <p> 451 452 <img src="{@docRoot}images/activity_lifecycle.png" alt="" /> 453 <p class="img-caption"><strong>1.</strong> .</p> 454 455 <p> 1, 456 457 , , 458 .</p> 459 460 <p class="table-caption"><strong>1.</strong> 461 .</p> 462 463 <table border="2" width="85%" frame="hsides" rules="rows"> 464 <colgroup align="left" span="3"></colgroup> 465 <colgroup align="left"></colgroup> 466 <colgroup align="center"></colgroup> 467 <colgroup align="center"></colgroup> 468 469 <thead> 470 <tr><th colspan="3"></th> <th></th> <th>?</th> <th></th></tr> 471 </thead> 472 473 <tbody> 474 <tr> 475 <td colspan="3" align="left"><code>{@link android.app.Activity#onCreate onCreate()}</code></td> 476 <td> . 477 — 478 , .. 479 Bundle, ( 480 ; . <a href="#actstate"> 481 </a>). 482 <p> {@code onStart()}.</p></td> 483 <td align="center"></td> 484 <td align="center">{@code onStart()}</td> 485 </tr> 486 487 <tr> 488 <td rowspan="5" style="border-left: none; border-right: none;"> </td> 489 <td colspan="2" align="left"><code>{@link android.app.Activity#onRestart 490 onRestart()}</code></td> 491 <td> 492 . 493 <p> {@code onStart()}.</p></td> 494 <td align="center"></td> 495 <td align="center">{@code onStart()}</td> 496 </tr> 497 498 <tr> 499 <td colspan="2" align="left"><code>{@link android.app.Activity#onStart onStart()}</code></td> 500 <td> , . 501 <p> {@code onResume()}, 502 , {@code onStop()}, .</p></td> 503 <td align="center"></td> 504 <td align="center">{@code onResume()} <br/><br/> {@code onStop()}</td> 505 </tr> 506 507 <tr> 508 <td rowspan="2" style="border-left: none;"> </td> 509 <td align="left"><code>{@link android.app.Activity#onResume onResume()}</code></td> 510 <td> , 511 . 512 , , . 513 <p> {@code onPause()}.</p></td> 514 <td align="center"></td> 515 <td align="center">{@code onPause()}</td> 516 </tr> 517 518 <tr> 519 <td align="left"><code>{@link android.app.Activity#onPause onPause()}</code></td> 520 <td>, 521 . 522 , , 523 .. , 524 , . 525 <p> {@code onResume()}, 526 , {@code onStop()}, 527 .</td> 528 <td align="center"><strong style="color:#800000"></strong></td> 529 <td align="center">{@code onResume()} <br/><br/> {@code onStop()}</td> 530 </tr> 531 532 <tr> 533 <td colspan="2" align="left"><code>{@link android.app.Activity#onStop onStop()}</code></td> 534 <td> , . 535 , , 536 ( ). 537 <p> {@code onRestart()}, 538 , 539 {@code onDestroy()}, .</p></td> 540 <td align="center"><strong style="color:#800000"></strong></td> 541 <td align="center">{@code onRestart()} <br/><br/> {@code onDestroy()}</td> 542 </tr> 543 544 <tr> 545 <td colspan="3" align="left"><code>{@link android.app.Activity#onDestroy 546 onDestroy()}</code></td> 547 <td> , . , 548 . 549 ( <code>{@link android.app.Activity#finish 550 finish()}</code>), 551 . 552 , <code>{@link 553 android.app.Activity#isFinishing isFinishing()}</code>.</td> 554 <td align="center"><strong style="color:#800000"></strong></td> 555 <td align="center"><em></em></td> 556 </tr> 557 </tbody> 558 </table> 559 560 <p> ? , 561 , , <em> </em> 562 . : ({@link 563 android.app.Activity#onPause 564 onPause()}, {@link android.app.Activity#onStop onStop()} {@link android.app.Activity#onDestroy 565 onDestroy()}). {@link android.app.Activity#onPause onPause()} 566 , {@link android.app.Activity#onPause onPause()} 567 , , <em> </em> ; 568 , {@link 569 android.app.Activity#onStop onStop()} {@link android.app.Activity#onDestroy onDestroy()} 570 . {@link android.app.Activity#onPause onPause()}, 571 ( ) . , 572 {@link android.app.Activity#onPause onPause()}, 573 574 .</p> 575 576 <p> , <b>?</b> , , 577 , . 578 {@link android.app.Activity#onPause onPause()} 579 {@link android.app.Activity#onResume onResume()}. , 580 {@link android.app.Activity#onPause onPause()}. </p> 581 582 <p class="note"><strong>.</strong> , 583 1, - , 584 , . , , 585 <a href="{@docRoot}guide/components/processes-and-threads.html"> 586 </a>.</p> 587 588 589 <h3 id="SavingActivityState"> </h3> 590 591 <p> <a href="#Lifecycle"> </a> , 592 593 . , 594 {@link android.app.Activity} - 595 , - . 596 , 597 ( ), .</p> 598 599 <p> , {@link 600 android.app.Activity} , 601 . {@link android.app.Activity}, 602 . , 603 , , , 604 , . 605 606 , : {@link 607 android.app.Activity#onSaveInstanceState onSaveInstanceState()}.</p> 608 609 <p> , 610 {@link android.app.Activity#onSaveInstanceState onSaveInstanceState()}. 611 {@link android.os.Bundle}, 612 -, , {@link 613 android.os.Bundle#putString putString()} {@link 614 android.os.Bundle#putInt putInt()}. , 615 , 616 {@link android.os.Bundle} : {@link android.app.Activity#onCreate onCreate()} {@link 617 android.app.Activity#onRestoreInstanceState onRestoreInstanceState()}. 618 {@link android.os.Bundle} 619 . , {@link 620 android.os.Bundle} ( , 621 ).</p> 622 623 <img src="{@docRoot}images/fundamentals/restore_instance.png" alt="" /> 624 <p class="img-caption"><strong> 2.</strong> 625 : , 626 , 627 .</p> 628 629 <p class="note"><strong>.</strong> , {@link 630 android.app.Activity#onSaveInstanceState onSaveInstanceState()} , 631 , , 632 (, <em></em>, 633 634 ). {@link android.app.Activity#onSaveInstanceState 635 onSaveInstanceState()}, {@link 636 android.app.Activity#onStop onStop()} , , {@link android.app.Activity#onPause 637 onPause()}.</p> 638 639 <p>, {@link 640 android.app.Activity#onSaveInstanceState onSaveInstanceState()}, 641 {@link 642 android.app.Activity#onSaveInstanceState onSaveInstanceState()} {@link android.app.Activity}. , 643 {@link 644 android.view.View#onSaveInstanceState onSaveInstanceState()} {@link 645 android.view.View} , , 646 . Android 647 , 648 . , {@link android.widget.EditText} , 649 , {@link android.widget.CheckBox} , 650 . ( <a href="{@docRoot}guide/topics/resources/layout-resource.html#idvalue">{@code android:id}</a>) 651 , . , 652 .</p> 653 654 <div class="sidebox-wrapper"> 655 <div class="sidebox"> 656 <p> . 657 {@link android.R.attr#saveEnabled android:saveEnabled} {@code "false"} 658 {@link android.view.View#setSaveEnabled setSaveEnabled()}. 659 , , .</p> 660 </div> 661 </div> 662 663 <p> {@link 664 android.app.Activity#onSaveInstanceState onSaveInstanceState()} 665 , - . 666 , , ( 667 , , , , 668 ).</p> 669 670 <p> {@link 671 android.app.Activity#onSaveInstanceState onSaveInstanceState()} , 672 , , - 673 674 {@link android.app.Activity#onSaveInstanceState onSaveInstanceState()}. {@link 675 android.app.Activity#onRestoreInstanceState onRestoreInstanceState()} , 676 .</p> 677 678 <p class="note"><strong>.</strong> {@link android.app.Activity#onSaveInstanceState 679 onSaveInstanceState()} , 680 ( 681 )— . {@link 682 android.app.Activity#onPause onPause()} (, , 683 ), .</p> 684 685 <p> 686 . 687 , , 688 . , 689 , 690 .</p> 691 692 693 <h3 id="ConfigurationChanges"> </h3> 694 695 <p> 696 (, , ). Android 697 ( {@link android.app.Activity#onDestroy}, {@link 698 android.app.Activity#onCreate onCreate()}). 699 700 , (, 701 ).</p> 702 703 <p> 704 , , 705 .</p> 706 707 <p> 708 {@link 709 android.app.Activity#onSaveInstanceState onSaveInstanceState()} {@link 710 android.app.Activity#onRestoreInstanceState onRestoreInstanceState()} ( {@link 711 android.app.Activity#onCreate onCreate()}), .</p> 712 713 <p> , , 714 <a href="{@docRoot}guide/topics/resources/runtime-changes.html"> 715 </a>.</p> 716 717 718 719 <h3 id="CoordinatingActivities"> </h3> 720 721 <p> , . 722 ( , - ), 723 . , , , , 724 , . 725 , 726 .</p> 727 728 <p> , , 729 , . , 730 . </p> 731 732 <ol> 733 <li> {@link android.app.Activity#onPause onPause()} .</li> 734 735 <li> {@link android.app.Activity#onCreate onCreate()}, {@link 736 android.app.Activity#onStart onStart()} {@link android.app.Activity#onResume onResume()} 737 . ( .)</li> 738 739 <li>, A , {@link 740 android.app.Activity#onStop onStop()}.</li> 741 </ol> 742 743 <p> 744 . , , 745 , 746 {@link android.app.Activity#onPause onPause()}, {@link 747 android.app.Activity#onStop onStop()}.</p> 748 749 <!-- 750 <h2>Beginner's Path</h2> 751 752 <p>For more information about how Android maintains a history of activities and 753 enables user multitasking, continue with the <b><a 754 href="{@docRoot}guide/components/tasks-and-back-stack.html">Tasks and Back 755 Stack</a></b> document.</p> 756 --> 757