Home | History | Annotate | Download | only in components
      1 page.title=
      2 page.tags=activity,intent
      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  1 
     55 
     56 
     57 
     58 
     59   [<em></em>] 
     60 
     61 <a href="{@docRoot}guide/components/tasks-and-back-stack.html"></a>
     62 
     63 </p>
     64 
     65 <p>   
     66 
     67 
     68 &mdash;&mdash;
     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   2 
     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   <a href="#Lifecycle"></a>
    106 </p>
    107 
    108 
    109 
    110 <h3 id="UI"> </h3>
    111 
    112 <p>   {@link android.view.View} &mdash;
    113  
    114 1 
    115 </p>
    116 
    117 <p>Android 
    118  
    119 {@link
    120 android.view.ViewGroup}   
    121 {@link android.view.View}  {@link android.view.ViewGroup} 
    122 
    123 </p>
    124 
    125 <p>XML   
    126   
    127 {@link android.app.Activity#setContentView(int) setContentView()}  ID  UI 
    128 
    129   {@link android.view.View} {@link android.view.ViewGroup}  {@link
    130 android.view.View}  {@link android.view.ViewGroup}  {@link android.app.Activity#setContentView(View)
    131 setContentView()} 
    132 
    133 </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 &lt;activity&gt;}</a>  <a href="{@docRoot}guide/topics/manifest/application-element.html">{@code &lt;application&gt;}</a> 
    143 
    144 </p>
    145 
    146 <pre>
    147 &lt;manifest ... &gt;
    148   &lt;application ... &gt;
    149       &lt;activity android:name=".ExampleActivity" /&gt;
    150       ...
    151   &lt;/application ... &gt;
    152   ...
    153 &lt;/manifest &gt;
    154 </pre>
    155 
    156 <p> UI <a href="{@docRoot}guide/topics/manifest/activity-element.html#nm">{@code android:name}</a> 
    157 
    158 
    159 &mdash;<a href="http://android-developers.blogspot.com/2011/06/things-that-cannot-change.html">Things That Cannot Change</a>
    160 
    161 
    162 </p>
    163 
    164 <p><a href="{@docRoot}guide/topics/manifest/activity-element.html">{@code &lt;activity&gt;}</a> 
    165 </p>
    166 
    167 
    168 <h4> </h4>
    169 
    170 <p><a href="{@docRoot}guide/topics/manifest/activity-element.html">{@code
    171 &lt;activity&gt;}</a>  &mdash; <a href="{@docRoot}guide/topics/manifest/intent-filter-element.html">{@code
    172 &lt;intent-filter&gt;}</a>   &mdash;  
    173 </p>
    174 
    175 <p>Android SDK   
    176 
    177  
    178 </p>
    179 
    180 <pre>
    181 &lt;activity android:name=".ExampleActivity" android:icon="@drawable/app_icon"&gt;
    182     &lt;intent-filter&gt;
    183         &lt;action android:name="android.intent.action.MAIN" /&gt;
    184         &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
    185     &lt;/intent-filter&gt;
    186 &lt;/activity&gt;
    187 </pre>
    188 
    189 <p><a href="{@docRoot}guide/topics/manifest/action-element.html">{@code
    190 &lt;action&gt;}</a>  <a href="{@docRoot}guide/topics/manifest/category-element.html">{@code
    191 &lt;category&gt;}</a>  
    192 </p>
    193 
    194 <p> 
    195  1 
    196  
    197 
    198 </p>
    199 
    200 <p> 
    201 
    202 <a href="{@docRoot}guide/topics/manifest/action-element.html">{@code
    203 &lt;action&gt;}</a>  <a href="{@docRoot}guide/topics/manifest/intent-filter-element.html">{@code
    204 &lt;intent-filter&gt;}</a>  <a href="{@docRoot}guide/topics/manifest/category-element.html">{@code
    205 &lt;category&gt;}</a>  <a href="{@docRoot}guide/topics/manifest/data-element.html">{@code
    206 &lt;data&gt;}</a> 
    207 
    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 1  {@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 &mdash;
    240 
    241  1 
    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>{@link android.app.Activity#startActivityForResult
    264   startActivityForResult()}{@link android.app.Activity#startActivity
    265   startActivity()} 
    266 
    267 {@link android.app.Activity#onActivityResult onActivityResult()}  
    268 {@link
    269 android.content.Intent}  {@link android.app.Activity#onActivityResult onActivityResult()} 
    270 </p>
    271 
    272 <p> 1 
    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 &#64;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 &amp;&amp; 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 1  {@code resultCode} {@link android.app.Activity#RESULT_OK} {@code requestCode}  {@link android.app.Activity#startActivityForResult
    302 startActivityForResult()}  2 &mdash;
    303 &mdash;&mdash;
    304 
    305  {@link android.content.Intent}{@code data} 
    306 </p>
    307 
    308 <p>{@link
    309 android.content.ContentResolver}   {@link android.database.Cursor} 
    310 <a href="{@docRoot}guide/topics/providers/content-providers.html"> </a>
    311 </p>
    312 
    313 <p><a href="{@docRoot}guide/components/intents-filters.html"> </a>
    314 </p>
    315 
    316 
    317 <h2 id="ShuttingDown"></h2>
    318 
    319 <p>{@link android.app.Activity#finish
    320 finish()} {@link android.app.Activity#finishActivity finishActivity()} 
    321 </p>
    322 
    323 <p class="note"><strong>:</strong> 
    324 Android 
    325 
    326 
    327 
    328 </p>
    329 
    330 
    331 <h2 id="Lifecycle"></h2>
    332 
    333 <p> 
    334 
    335 
    336 </p>
    337 
    338 <p> 3 </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}  <em></em>
    355 
    356 
    357 </dd>
    358 </dl>
    359 
    360 <p>{@link android.app.Activity#finish finish()} 
    361 
    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     &#64;Override
    378     public void {@link android.app.Activity#onCreate onCreate}(Bundle savedInstanceState) {
    379         super.onCreate(savedInstanceState);
    380         // The activity is being created.
    381     }
    382     &#64;Override
    383     protected void {@link android.app.Activity#onStart onStart()} {
    384         super.onStart();
    385         // The activity is about to become visible.
    386     }
    387     &#64;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     &#64;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     &#64;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     &#64;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> 3 
    414  </p>
    415 
    416 <ul>
    417 <li><b> entire lifetime</b> {@link
    418 android.app.Activity#onCreate onCreate()} {@link
    419 android.app.Activity#onDestroy} {@link android.app.Activity#onCreate onCreate()} {@link android.app.Activity#onDestroy} 
    420 
    421  {@link android.app.Activity#onCreate onCreate()} {@link
    422 android.app.Activity#onDestroy} 
    423 
    424 </li>
    425 
    426 <li><p><b>visible lifetime</b> {@link
    427 android.app.Activity#onStart onStart()} {@link
    428 android.app.Activity#onStop onStop()} 
    429  {@link android.app.Activity#onStop onStop()} 
    430  2 
    431 {@link android.content.BroadcastReceiver}  {@link
    432 android.app.Activity#onStart onStart()}  UI  {@link android.app.Activity#onStop onStop()} 
    433 
    434 
    435  entire lifetime  {@link android.app.Activity#onStart onStart()}  {@link
    436 android.app.Activity#onStop onStop()} 
    437 </p></li>
    438 
    439 <li><p> <b>foreground lifetime</b>  {@link
    440 android.app.Activity#onResume onResume()} {@link android.app.Activity#onPause
    441 onPause()} 
    442  {@link android.app.Activity#onPause onPause()} 
    443 &mdash;
    444  2 
    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       &mdash;
    478  Bundle <a href="#actstate"></a>
    479 
    480 
    481 
    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;">&nbsp;&nbsp;&nbsp;&nbsp;</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()}  {@code onStop()} 
    502 </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;">&nbsp;&nbsp;&nbsp;&nbsp;</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  CPU 
    522 
    523 
    524 
    525        <p> {@code onResume()}  {@code onStop()} 
    526 
    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()}  {@code onDestroy()} 
    538 
    539 </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 <code>{@link android.app.Activity#finish
    549        finish()}</code> 
    550 
    551  2 <code>{@link
    552        android.app.Activity#isFinishing isFinishing()}</code> 
    553 </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><em></em>
    561 
    562 3 {@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()}  3 {@link android.app.Activity#onPause onPause()} <em></em>{@link
    566 android.app.Activity#onStop onStop()}  {@link android.app.Activity#onDestroy onDestroy()} 
    567 
    568 &mdash;
    569 
    570 {@link android.app.Activity#onPause onPause()} 
    571 {@link android.app.Activity#onPause onPause()} 
    572 
    573 
    574 </p>
    575 
    576 <p> <b></b>
    577 {@link android.app.Activity#onPause onPause()} {@link android.app.Activity#onResume onResume()} 
    578 
    579 {@link android.app.Activity#onPause onPause()} 
    580  </p>
    581 
    582 <p class="note"><strong>:</strong>  1 
    583 &mdash;
    584 <a href="{@docRoot}guide/components/processes-and-threads.html">Processes and Threading</a> 
    585 
    586 </p>
    587 
    588 
    589 <h3 id="SavingActivityState"></h3>
    590 
    591 <p><a href="#Lifecycle"></a>
    592 
    593  {@link android.app.Activity}  &mdash; 
    594 
    595 
    596 
    597 </p>
    598 
    599 <p> {@link
    600 android.app.Activity} 
    601  {@link android.app.Activity} 
    602 
    603 
    604  {@link
    605 android.app.Activity#onSaveInstanceState onSaveInstanceState()}
    606 
    607 </p>
    608 
    609 <p> {@link android.app.Activity#onSaveInstanceState onSaveInstanceState()} 
    610  {@link android.os.Bundle}  {@link
    611 android.os.Bundle#putString putString()}  {@link
    612 android.os.Bundle#putInt putInt()} 
    613 
    614   {@link android.os.Bundle}  {@link android.app.Activity#onCreate onCreate()}  {@link
    615 android.app.Activity#onRestoreInstanceState onRestoreInstanceState()} 
    616 
    617  {@link android.os.Bundle} 
    618 
    619 {@link
    620 android.os.Bundle}  null 
    621 </p>
    622 
    623 <img src="{@docRoot}images/fundamentals/restore_instance.png" alt="" />
    624 <p class="img-caption"><strong> 2.</strong>   2 
    625 
    626 
    627 </p>
    628 
    629 <p class="note"><strong>:</strong>  {@link
    630 android.app.Activity#onSaveInstanceState onSaveInstanceState()}  [<em></em>] 
    631 
    632 
    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()} {@link android.app.Activity}  {@link
    641 android.app.Activity#onSaveInstanceState onSaveInstanceState()} 
    642  {@link
    643 android.view.View}  {@link
    644 android.view.View#onSaveInstanceState onSaveInstanceState()} 
    645 
    646 Android UI 
    647 
    648 {@link android.widget.EditText} {@link android.widget.CheckBox} 
    649 
    650  ID<a href="{@docRoot}guide/topics/resources/layout-resource.html#idvalue">{@code android:id}</a>
    651  ID 
    652 </p>
    653 
    654 <div class="sidebox-wrapper">
    655 <div class="sidebox">
    656 <p>{@link android.R.attr#saveEnabled android:saveEnabled}  {@code "false"} {@link android.view.View#setSaveEnabled setSaveEnabled()} 
    657 
    658  UI 
    659 </p>
    660 </div>
    661 </div>
    662 
    663 <p>{@link
    664 android.app.Activity#onSaveInstanceState onSaveInstanceState()}  UI UI  UI 
    665 
    666 
    667 
    668 </p>
    669 
    670 <p>{@link
    671 android.app.Activity#onSaveInstanceState onSaveInstanceState()}  UI  {@link android.app.Activity#onSaveInstanceState onSaveInstanceState()} 
    672 
    673 
    674 {@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 &mdash; {@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  {@link android.app.Activity#onDestroy}  {@link
    697 android.app.Activity#onCreate onCreate()}
    698 
    699 
    700 
    701 </p>
    702 
    703 <p>
    704 
    705 </p>
    706 
    707 <p>{@link
    708   android.app.Activity#onSaveInstanceState onSaveInstanceState()}  {@link
    709 android.app.Activity#onRestoreInstanceState onRestoreInstanceState()} {@link
    710 android.app.Activity#onCreate onCreate()}
    711 </p>
    712 
    713 <p><a href="{@docRoot}guide/topics/resources/runtime-changes.html"></a>
    714 
    715 </p>
    716 
    717 
    718 
    719 <h3 id="CoordinatingActivities"></h3>
    720 
    721  <p>1 1 
    722 
    723 2  1 2 1 
    724 
    725 
    726 </p>
    727 
    728 <p> 2  1  
    729  A  B 
    730  </p>
    731 
    732 <ol>
    733 <li> A  {@link android.app.Activity#onPause onPause()} </li>
    734 
    735 <li> B  {@link android.app.Activity#onCreate onCreate()}{@link
    736 android.app.Activity#onStart onStart()}{@link android.app.Activity#onResume onResume()} 
    737   B </li>
    738 
    739 <li> A {@link
    740 android.app.Activity#onStop onStop()} </li>
    741 </ol>
    742 
    743  <p> 1 
    744 1  {@link
    745 android.app.Activity#onStop onStop()}  {@link android.app.Activity#onPause onPause()} 
    746 
    747 </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