Sie sind auf Seite 1von 5

Developers

Training

API Guides

Design

Reference

Getting Started

Develop

Tools

Console

Distribute

Google Services

Samples

Preview

Pausing and Resuming an Activity

Building Your First App


Supporting Different Devices
Managing the Activity Lifecycle
Starting an Activity
Pausing and Resuming an Activity

During normal app use, the foreground activity is


sometimes obstructed by other visual components that
cause the activity to pause. For example, when a semitransparent activity opens (such as one in the style of a
dialog), the previous activity pauses. As long as the
activity is still partially visible but currently not the
activity in focus, it remains paused.

Stopping and Restarting an Activity

However, once the activity is fully-obstructed and not visible, it

Recreating an Activity

stops (which is discussed in the next lesson).

Building a Dynamic UI with Fragments


Saving Data

As your activity enters the paused state, the system calls the
onPause() method on your Activity, which allows you to

stop ongoing actions that should not continue while paused


(such as a video) or persist any information that should be

Interacting with Other Apps

Previous

Next

This lesson teaches you to


Pause Your Activity
Resume Your Activity

You should also read


Activities

Try it out
DOWNLOAD THE DEMO
ActivityLifecycle.zip

permanently saved in case the user continues to leave your app.


If the user returns to your activity from the paused state, the

Working with System Permissions


Building Apps with

system resumes it and calls the onResume() method.


Note: When your activity receives a call to onPause(), it may be an indication that the activity will be

Content Sharing

paused for a moment and the user may return focus to your activity. However, it's usually the first indication
that the user is leaving your activity.

Building Apps with


Multimedia
Building Apps with
Graphics & Animation
Building Apps with
Connectivity & the Cloud
Building Apps with
Location & Maps
Building Apps with
User Info & Sign-In
Building Apps for
Wearables

Figure 1. When a semi-transparent activity obscures your activity, the system calls onPause() and the activity waits in
the Paused state (1). If the user returns to the activity while it's still paused, the system calls onResume() (2).

Building Apps for


TV

Pause Your Activity

Building Apps for


Auto
Building Apps for
Work

When the system calls onPause() for your activity, it technically means your activity is still partially visible, but
most often is an indication that the user is leaving the activity and it will soon enter the Stopped state. You
should usually use the onPause() callback to:
Stop animations or other ongoing actions that could consume CPU.

Best Practices for


Interaction & Engagement

Commit unsaved changes, but only if users expect such changes to be permanently saved when they leave
(such as a draft email).

Best Practices for


User Interface
Best Practices for
User Input
Best Practices for
Background Jobs
Best Practices for
Performance
Best Practices for
Security & Privacy
Best Practices for
Permissions & Identifiers

Release system resources, such as broadcast receivers, handles to sensors (like GPS), or any resources that
may affect battery life while your activity is paused and the user does not need them.
For example, if your application uses the Camera, the onPause() method is a good place to release it.
@Override
public void onPause() {
super.onPause(); // Always call the superclass method first
// Release the Camera because we don't need it when paused
// and other activities might need to use it.
if (mCamera != null) {
mCamera.release();
mCamera = null;
}
}

Generally, you should not use onPause() to store user changes (such as personal information entered into a
form) to permanent storage. The only time you should persist user changes to permanent storage within

Best Practices for


Testing
Using Google Play to
Distribute & Monetize

onPause() is when you're certain users expect the changes to be auto-saved (such as when drafting an email).

However, you should avoid performing CPU-intensive work during onPause(), such as writing to a database,
because it can slow the visible transition to the next activity (you should instead perform heavy-load shutdown
operations during onStop()).
You should keep the amount of operations done in the onPause() method relatively simple in order to allow for
a speedy transition to the user's next destination if your activity is actually being stopped.
Note: When your activity is paused, the Activity instance is kept resident in memory and is recalled when
the activity resumes. You dont need to re-initialize components that were created during any of the callback
methods leading up to the Resumed state.

Resume Your Activity

Resume Your Activity


When the user resumes your activity from the Paused state, the system calls the onResume() method.
Be aware that the system calls this method every time your activity comes into the foreground, including when
it's created for the first time. As such, you should implement onResume() to initialize components that you
release during onPause() and perform any other initializations that must occur each time the activity enters
the Resumed state (such as begin animations and initialize components only used while the activity has user
focus).
The following example of onResume() is the counterpart to the onPause() example above, so it initializes the
camera that's released when the activity pauses.
@Override
public void onResume() {
super.onResume(); // Always call the superclass method first
// Get the Camera instance as the activity achieves full user focus
if (mCamera == null) {
initializeCamera(); // Local method to handle camera init
}
}

Next: Stopping and Restarting an Activity

Get news & tips

Blog Support

Except as noted, this content is licensed under Creative Commons Attribution 2.5. For details and restrictions, see the Content
License.
About Android | Auto | TV | Wear | Legal

English

Das könnte Ihnen auch gefallen