• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1page.title=Pausing and Resuming an Activity
2parent.title=Managing the Activity Lifecycle
3parent.link=index.html
4
5trainingnavtop=true
6previous.title=Starting an Activity
7previous.link=starting.html
8next.title=Stopping and Restarting an Activity
9next.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"
32class="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
40visual components that cause the activity to <em>pause</em>.  For example, when a semi-transparent
41activity opens (such as one in the style of a dialog), the previous activity pauses. As long as the
42activity 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
45discussed in the next lesson).</p>
46
47<p>As your activity enters the paused state, the system calls the {@link
48android.app.Activity#onPause onPause()} method on your {@link android.app.Activity}, which allows
49you to stop ongoing actions that should not continue while paused (such as a video) or persist
50any information that should be permanently saved in case the user continues to leave your app. If
51the 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
55android.app.Activity#onPause()}, it may be an indication that the activity will be paused for a
56moment and the user may return focus to your activity. However, it's usually the first indication
57that 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
61your activity, the system calls {@link android.app.Activity#onPause onPause()} and the activity
62waits in the Paused state (1). If the user returns to the activity while it's still paused, the
63system 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
69technically means your activity is still partially visible, but most often is an indication that
70the user is leaving the activity and it will soon enter the Stopped state.  You should usually use
71the {@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
76they leave (such as a draft email).</li>
77  <li>Release system resources, such as broadcast receivers, handles to sensors (like
78GPS), or any resources that may affect battery life while your activity is paused and the user
79does 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
87public 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
100user changes (such as personal information entered into a form) to permanent storage. The only time
101you should persist user changes to permanent storage within {@link android.app.Activity#onPause()}
102is when you're certain users expect the changes to be auto-saved (such as when drafting an email).
103However, you should avoid performing CPU-intensive work during {@link
104android.app.Activity#onPause()}, such as writing to a database, because it can slow the visible
105transition 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
109onPause()} method relatively simple in order to allow for a speedy transition to the user's next
110destination if your activity is actually being stopped.</p>
111
112<p class="note"><strong>Note:</strong> When your activity is paused, the {@link
113android.app.Activity} instance is kept resident in memory and is recalled when the activity resumes.
114You don’t need to re-initialize components that were created during any of the callback methods
115leading 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
122android.app.Activity#onResume()} method.</p>
123
124<p>Be aware that the system calls this method every time your activity comes into the foreground,
125including when it's created for the first time. As such, you should implement {@link
126android.app.Activity#onResume()} to initialize components that you release during {@link
127android.app.Activity#onPause()} and perform any other initializations that must occur each time the
128activity enters the Resumed state (such as begin animations and initialize components only used
129while the activity has user focus).</p>
130
131<p>The following example of {@link android.app.Activity#onResume()} is the counterpart to
132the {@link android.app.Activity#onPause()} example above, so it initializes the camera that's
133released when the activity pauses.</p>
134
135<pre>
136&#64;Override
137public 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