Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to pause and resume Activity Android

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces you how to suspend and resume Activity Android, the content is very detailed, interested friends can refer to, hope to be helpful to you.

Pause and resume Activity (Pausing and Resuming an Activity)

In normal application use, the foreground activity is sometimes obscured by other visual components, resulting in activity pauses. For example, when a translucent activity is opened (such as in a style dialog box), the previous activity is paused. As long as the activity is still partially visible, but not currently in focus, it remains in a paused state.

However, once the activity is completely obscured and invisible to the user, it stops (which is what we will discuss in the next lesson).

When your activity enters a paused state, the system will call the onPause () method on your Activity. In this method, you can stop behaviors that should not continue while paused (such as video playback), or persist some information that needs to be saved so that users can continue to stay in your application. If the user returns to your activity, the system resumes it from the paused state and calls the onResume () method.

Note: when your activity receives a call to onPause (), it may be a sign that activity will pause for a while, and then the user will return to your activity. However, it is usually a sign that the user is leaving your activity.

Figure 1. When a translucent activity masks your activity, the system will call onPause (), activity, etc., in a paused state (1). If the user returns to the activity that is still paused, the system calls onResume () (2).

Pause your Activity (Pause Your Activity)

When the system calls onPause () for your activity, technically, your activity is still partially visible, but the most common is to indicate that the user is leaving the activity, and it will soon enter a Stopped state. You should usually use the onPause () callback:

Stop animation effects or other ongoing behaviors that consume CPU resources.

Commit unsaved changes, but only if users want to do this: when they leave, they need to save the changes (such as composing an email).

Release some system resources, such as broadcast receivers, processing sensors (such as GPS), or any resources that affect battery life, while your activity is paused and users no longer need them.

For example, if your application uses a 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;}}

In general, you should not use onPause () to save user changes (such as personal information input forms) to persistent storage. Only when you are sure that the user expects to automatically save their changes (such as drafting an email), you can write the user's changes to persistent storage in the onPause () method. However, you should avoid performing heavy CPU tasks in onPause (), such as reading and writing to the database, as it slows down to the transition to the next activity (you should perform a heavy load shutdown in onStop ()).

You should keep the volume of business relatively simple in the onPause () method in order to make a quick transition to the user's next destination if your activity is actually stopped.

Note: when your activity is paused, the Activity instance still resides in memory and is called again when the activity is restored. When you call any callback methods to transition to the recovery state, you do not need to reinitialize the components that have been created.

Restore your Activity (Resume Your Activity)

When the user resumes your activity from the paused state, the system calls the onResume () method.

Note that this method is called every time your activity enters the foreground, including when it is created * times. Therefore, when implementing onResume (), you should initialize the components that you will release in onPause () and perform initialization actions that must be completed every time activity enters the recovery state (such as starting animation and initializing components that activity needs to use to get user focus).

The following example onResume () is the corresponding onPause () example above, so it initializes the camera that is released when activity is paused.

@ 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}} so much about how to pause and resume Activity Android. I hope the above can be helpful and learn more. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report