Home | History | Annotate | Download | only in activity-lifecycle
      1 page.title=Pausing and Resuming an Activity
      2 parent.title=Managing the Activity Lifecycle
      3 parent.link=index.html
      4 
      5 trainingnavtop=true
      6 previous.title=Starting an Activity
      7 previous.link=starting.html
      8 next.title=Stopping and Restarting an Activity
      9 next.link=stopping.html
     10 
     11 @jd:body
     12 
     13 <div id="tb-wrapper">
     14   <div id="tb">
     15     
     16     <h2>This lesson teaches you to</h2>
     17     <ol>
     18       <li><a href="#Pause">Pause Your Activity</a></li>
     19       <li><a href="#Resume">Resume Your Activity</a></li>
     20     </ol>
     21     
     22     <h2>You should also read</h2>
     23     <ul>
     24       <li><a href="{@docRoot}guide/components/activities.html">Activities</a>
     25       </li>
     26     </ul>
     27 
     28 <h2>Try it out</h2>
     29 
     30 <div class="download-box">
     31  <a href="http://developer.android.com/shareables/training/ActivityLifecycle.zip"
     32 class="button">Download the demo</a>
     33  <p class="filename">ActivityLifecycle.zip</p>
     34 </div>
     35 
     36   </div>
     37 </div>
     38 
     39 <p>During normal app use, the foreground activity is sometimes obstructed by other 
     40 visual components that cause the activity to <em>pause</em>.  For example, when a semi-transparent
     41 activity opens (such as one in the style of a dialog), the previous activity pauses. As long as the
     42 activity is still partially visible but currently not the activity in focus, it remains paused.</p>
     43 
     44 <p>However, once the activity is fully-obstructed and not visible, it <em>stops</em> (which is
     45 discussed in the next lesson).</p>
     46 
     47 <p>As your activity enters the paused state, the system calls the {@link
     48 android.app.Activity#onPause onPause()} method on your {@link android.app.Activity}, which allows
     49 you to stop ongoing actions that should not continue while paused (such as a video) or persist
     50 any information that should be permanently saved in case the user continues to leave your app. If
     51 the user returns to your activity from the paused state, the system resumes it and calls the
     52 {@link android.app.Activity#onResume onResume()} method.</p>
     53 
     54 <p class="note"><strong>Note:</strong> When your activity receives a call to {@link
     55 android.app.Activity#onPause()}, it may be an indication that the activity will be paused for a
     56 moment and the user may return focus to your activity. However, it's usually the first indication
     57 that the user is leaving your activity.</p>
     58 
     59 <img src="{@docRoot}images/training/basics/basic-lifecycle-paused.png" />
     60 <p class="img-caption"><strong>Figure 1.</strong> When a semi-transparent activity obscures
     61 your activity, the system calls {@link android.app.Activity#onPause onPause()} and the activity
     62 waits in the Paused state (1). If the user returns to the activity while it's still paused, the
     63 system calls {@link android.app.Activity#onResume onResume()} (2).</p>
     64 
     65 
     66 <h2 id="Pause">Pause Your Activity</h2>
     67       
     68 <p>When the system calls {@link android.app.Activity#onPause()} for your activity, it
     69 technically means your activity is still partially visible, but most often is an indication that
     70 the user is leaving the activity and it will soon enter the Stopped state.  You should usually use
     71 the {@link android.app.Activity#onPause()} callback to:</p>
     72 
     73 <ul>
     74   <li>Stop animations or other ongoing actions that could consume CPU.</li>
     75   <li>Commit unsaved changes, but only if users expect such changes to be permanently saved when
     76 they leave (such as a draft email).</li>
     77   <li>Release system resources, such as broadcast receivers, handles to sensors (like
     78 GPS), or any resources that may affect battery life while your activity is paused and the user
     79 does not need them.</li>
     80 </ul>
     81 
     82 <p>For example, if your application uses the {@link android.hardware.Camera}, the
     83 {@link android.app.Activity#onPause()} method is a good place to release it.</p>
     84 
     85 <pre>
     86 &#64;Override
     87 public void onPause() {
     88     super.onPause();  // Always call the superclass method first
     89 
     90     // Release the Camera because we don't need it when paused
     91     // and other activities might need to use it.
     92     if (mCamera != null) {
     93         mCamera.release()
     94         mCamera = null;
     95     }
     96 }
     97 </pre>
     98 
     99 <p>Generally, you should <strong>not</strong> use {@link android.app.Activity#onPause()} to store
    100 user changes (such as personal information entered into a form) to permanent storage. The only time
    101 you should persist user changes to permanent storage within {@link android.app.Activity#onPause()}
    102 is when you're certain users expect the changes to be auto-saved (such as when drafting an email).
    103 However, you should avoid performing CPU-intensive work during {@link
    104 android.app.Activity#onPause()}, such as writing to a database, because it can slow the visible
    105 transition to the next activity (you should instead perform heavy-load shutdown operations during
    106 {@link android.app.Activity#onStop onStop()}).</p>
    107 
    108 <p>You should keep the amount of operations done in the {@link android.app.Activity#onPause
    109 onPause()} method relatively simple in order to allow for a speedy transition to the user's next
    110 destination if your activity is actually being stopped.</p>
    111 
    112 <p class="note"><strong>Note:</strong> When your activity is paused, the {@link
    113 android.app.Activity} instance is kept resident in memory and is recalled when the activity resumes.
    114 You dont need to re-initialize components that were created during any of the callback methods
    115 leading up to the Resumed state.</p>
    116 
    117 
    118 
    119 <h2 id="Resume">Resume Your Activity</h2>
    120 
    121 <p>When the user resumes your activity from the Paused state, the system calls the {@link
    122 android.app.Activity#onResume()} method.</p>
    123 
    124 <p>Be aware that the system calls this method every time your activity comes into the foreground,
    125 including when it's created for the first time. As such, you should implement {@link
    126 android.app.Activity#onResume()} to initialize components that you release during {@link
    127 android.app.Activity#onPause()} and perform any other initializations that must occur each time the
    128 activity enters the Resumed state (such as begin animations and initialize components only used
    129 while the activity has user focus).</p>
    130 
    131 <p>The following example of {@link android.app.Activity#onResume()} is the counterpart to
    132 the {@link android.app.Activity#onPause()} example above, so it initializes the camera that's
    133 released when the activity pauses.</p>
    134 
    135 <pre>
    136 &#64;Override
    137 public void onResume() {
    138     super.onResume();  // Always call the superclass method first
    139 
    140     // Get the Camera instance as the activity achieves full user focus
    141     if (mCamera == null) {
    142         initializeCamera(); // Local method to handle camera init
    143     }
    144 }
    145 </pre>
    146 
    147 
    148 
    149 
    150 
    151 
    152 
    153