Android官方入门文档[13]暂停和恢复一个Activity活动

简介: Android官方入门文档[13]暂停和恢复一个Activity活动Pausing and Resuming an Activity暂停和恢复一个Activity活动 This lesson teaches you to1.

Android官方入门文档[13]暂停和恢复一个Activity活动


Pausing and Resuming an Activity
暂停和恢复一个Activity活动

 

This lesson teaches you to
1.Pause Your Activity
2.Resume Your Activity

You should also read
•Activities
这节课教你
1.暂停您的Activity活动
2.恢复您的Activity活动

你也应该阅读
•Activity活动

Try it out
试试吧

Download the demo
ActivityLifecycle.zip
下载演示
ActivityLifecycle.zip

During normal app use, the foreground activity is sometimes obstructed by other visual components that cause the activity to pause. For example, when a semi-transparent 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.
在正常的应用程序使用时,前台Activity活动有时通过致使所述activity暂停等可视部件阻碍。例如,当一个半透明activity打开(诸如一个在一个对话的方式),所述先前Activity活动暂停。只要Activity活动仍部分地可见的,但目前未处于焦点中的Activity活动,它保持暂停状态。

However, once the activity is fully-obstructed and not visible, it stops (which is discussed in the next lesson).
然而,一旦活性完全阻塞和不可见的,它停止(这将在下一课讨论)。

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 permanently saved in case the user continues to leave your app. If the user returns to your activity from the paused state, the system resumes it and calls the onResume() method.
为您的Activity活动进入暂停状态,系统调用的onPause()方法的Activity活动,它允许你停止不应该继续暂停时(如视频)正在进行的行动或持续应永久保存在任何情况下,信息用户继续留下您的应用程序。如果用户返回到从暂停状态的activity,系统恢复,并调用onResume()方法。

Note: When your activity receives a call to onPause(), it may be an indication that the activity will be 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.
注意:当你的Activity活动接接收一个调用onPause(),它可能是一个迹象,该Activity活动将被暂停了一会儿,用户可以焦点返回到你的Activity活动。然而,这通常是第一个迹象表明,用户离开你的Activity活动。

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).
图1.当一个半透明activity掩盖了你的Activity活动,系统调用的onPause()和暂停状态的activity等待(1)。如果用户返回到该Activity活动,而它仍然暂停,系统调用onResume()(2)。

 

Pause Your Activity
暂停你的Activity活动


--------------------------------------------------------------------------------

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.
•Commit unsaved changes, but only if users expect such changes to be permanently saved when they leave (such as a draft email).
•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.
当系统调用的onPause()为您的Activity活动,这在技术上意味着你的Activity活动仍是部分可见,但大多数情况下是显示用户留下activity,它会很快进入停止状态。通常你应该使用的onPause()回调至:
•停止动画或可能消耗CPU其他正在进行的行动。
•提交未保存的更改,但前提是用户希望这样的改变,当他们离开(如电子邮件草稿)被永久保存。
•释放系统资源,如广播接收器,手柄传感器(如GPS),或可能影响电池寿命,同时您的Activity活动将暂停,用户不需要他们的任何资源。

For example, if your application uses the Camera, the onPause() method is a good place to release it.
例如,如果你的应用程序使用相机时,在onPause()方法是一个很好的地方,将其释放。

@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 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()).
通常情况下,你不应该使用的onPause()来存储用户的变化(如进入了一个形式的个人信息)到永久存储。你应该坚持在onPause()内用户更改为永久存储是仅当你某些用户期望的变化是自动保存(比如,起草一封电子邮件时)。然而,你应该避免的onPause()期间执行CPU密集型的工作,如写入数据库,因为它可以减缓可见过渡到下一个Activity活动(你应该不是的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.
你应该保持,以便允许为迅速过渡到用户的下一个目的地,如果您的activity实际上正在停在的onPause()方法比较简单进行操作的量。

Note: When your activity is paused, the Activity instance is kept resident in memory and is recalled when the activity resumes. You don’t need to re-initialize components that were created during any of the callback methods leading up to the Resumed state.
注意:当你的Activity活动暂停,Activity活动实例保持驻留在内存中的Activity活动恢复的时候被调用。你不需要重新初始化过程中的任何的回调方法导致对续状态中创建的组件。

 

Resume Your Activity
恢复您的Activity活动


--------------------------------------------------------------------------------

When the user resumes your activity from the Paused state, the system calls the onResume() method.
当用户从恢复暂停状态的activity时,系统调用onResume()方法。

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).
请注意,系统调用这个方法每一个你的Activity活动进入前台,当它第一次创建包括时间。因此,你应该实现onResume()来初始化你的onPause()期间释放组件和执行必须发生的每个该Activity活动每次进入恢复状态任何其他初始化(如开始动画和初始化组件仅用于而Activity活动有用户焦点)。

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.
onResume()的下面的例子是对应到的onPause()上面的例子,所以它的初始化的时候发布该Activity活动暂停相机。

@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
下一页:停止和重新启动的Activity活动

本文翻译自:https://developer.android.com/training/basics/activity-lifecycle/pausing.html

目录
相关文章
|
3月前
|
Android开发 开发者
Android UI设计: 请解释Activity的Theme是什么,如何更改应用程序的主题?
Android UI设计: 请解释Activity的Theme是什么,如何更改应用程序的主题?
40 1
|
1月前
|
测试技术 API 调度
【Android 从入门到出门】第七章:开始使用WorkManager
【Android 从入门到出门】第七章:开始使用WorkManager
19 3
【Android 从入门到出门】第七章:开始使用WorkManager
|
1月前
|
存储 Android开发 C++
【Android 从入门到出门】第五章:使用DataStore存储数据和测试
【Android 从入门到出门】第五章:使用DataStore存储数据和测试
29 3
|
1月前
|
Android开发
【Android 从入门到出门】第四章:现代Android开发中的导航
【Android 从入门到出门】第四章:现代Android开发中的导航
22 2
【Android 从入门到出门】第四章:现代Android开发中的导航
|
1月前
|
XML API Android开发
【Android 从入门到出门】第三章:使用Hilt处理Jetpack Compose UI状态
【Android 从入门到出门】第三章:使用Hilt处理Jetpack Compose UI状态
26 4
|
1月前
|
存储 XML 编译器
【Android 从入门到出门】第二章:使用声明式UI创建屏幕并探索组合原则
【Android 从入门到出门】第二章:使用声明式UI创建屏幕并探索组合原则
47 3
|
1月前
|
存储 SQL 数据库
【Android 从入门到出门】第六章:使用Room数据库并测试
【Android 从入门到出门】第六章:使用Room数据库并测试
29 4
|
1月前
|
存储 Android开发
【Android 从入门到出门】第八章:分页入门指南
【Android 从入门到出门】第八章:分页入门指南
21 3
|
1月前
|
IDE Java 开发工具
【Android 从入门到出门】第一章:Android开发技能入门指南
【Android 从入门到出门】第一章:Android开发技能入门指南
60 3
|
2月前
|
JSON Java Go