[Andriod官方训练教程]管理Activity的生命活动之暂停和恢复一个Activity

原文地址:https://developer.android.com/training/basics/activity-lifecycle/pausing.html

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

During normal app use, the foreground activity is sometimes obstructed by other visual components that cause the activity topause. 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.

在通常的app使用中,前台的activity有时会被另一个可见的部件阻塞从而引起该activity发生暂停。例如,当一个半透明的activity打开时(比如某种类型的对话框),那么之前的activity就暂停了。

However, once the activity is fully-obstructed and not visible, it stops (which is discussed in the next lesson).

但是,一旦activity被完全阻塞并且不再可见,它将会停止(在下一节课里讨论)。

As your activity enters the paused state, the system calls the onPause() method on yourActivity, 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 theonResume() method.

当你的activty进入暂停阶段,系统在你的Activity中调用onPause()方法,这允许你停止正在进行的动作,而这些动作不应该在暂停时继续进行(例如一个视频),或是在用户离开你的app时保留任何应该永久保存的信息。如果用户从暂停状态返回到你的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 callsonPause() and the activity waits in the Paused state (1). If the user returns to the activity while it's still paused, the system callsonResume() (2).

图1.当一个半透明的acti阻塞你的activity时,系统调用onPause(),然后activity将在Paused状态等候(1)。如果在它暂停时,用户返回到activity,系统将调用onResume() (2)。

 

Pause Your 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 theonPause() 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.

当系统为你的activity调用onPause()时,在技术上意味着你的activity仍是部分可见的,但更多的情况下这意味着用户正在离开activity,并且将到达Stopped状态。通常情况下,你应该用onPause()回调函数来:

  • 停止动画或其他正在进行的会消耗CPU的动作。
  • 提交为保存的变化,但是只在当他们离开并且期望这些变化被永久保存时(例如邮件草稿)。
  • 释放用户不需要的系统资源,例如广播收音机,传感器手柄(像GPS),或是任何可能影响电池寿命的资源。

For example, if your application uses the Camera, theonPause() method is a good place to release it.

例如,如果你的应用使用CameraonPause()方法是一个释放它的好地方。

@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.

你应该保持onPause()中完成的操作的数目相对简单,以便当你的activity真的需要停止时,可以允许快速地向用户的下一个目标转换。

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 —— 恢复你的活动


 

When the user resumes your activity from the Paused state, the system calls theonResume() method.

当用户从Paused状态返回你的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 implementonResume() to initialize components that you release duringonPause() 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()释放的部件,并且执行那些在进入Resumed状态时必须出现的初始化动作(例如开始动画,初始化那些当activity获得用户聚焦时需要的部件)。

The following example of onResume() is the counterpart to theonPause() 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
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值