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