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