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

相关文章
探索Android开发:从入门到精通的旅程
在这篇文章中,我们将一起踏上一段激动人心的旅程,通过深入浅出的方式,解锁Android开发的秘密。无论你是编程新手还是有经验的开发者,本文都将为你提供宝贵的知识和技能,帮助你构建出色的Android应用。我们将从基础概念开始,逐步深入到高级技巧和最佳实践,最终实现从初学者到专家的转变。让我们开始吧!
83 3
Android面试之Activity启动流程简述
Android面试之Activity启动流程简述
123 6
Android面试高频知识点(4) 详解Activity的启动流程
Android面试高频知识点(4) 详解Activity的启动流程
44 3
Android实战之如何截取Activity或者Fragment的内容?
本文首发于公众号“AntDream”,介绍了如何在Android中截取Activity或Fragment的屏幕内容并保存为图片。包括截取整个Activity、特定控件或区域的方法,以及处理包含RecyclerView的复杂情况。
44 3
Android kotlin MVVM 架构简单示例入门
Android kotlin MVVM 架构简单示例入门
66 1
安卓与iOS开发中的跨平台解决方案:Flutter入门
【9月更文挑战第30天】在移动应用开发的广阔舞台上,安卓和iOS两大操作系统各自占据半壁江山。开发者们常常面临着选择:是专注于单一平台深耕细作,还是寻找一种能够横跨两大系统的开发方案?Flutter,作为一种新兴的跨平台UI工具包,正以其现代、响应式的特点赢得开发者的青睐。本文将带你一探究竟,从Flutter的基础概念到实战应用,深入浅出地介绍这一技术的魅力所在。
153 7
|
4月前
|
安卓应用开发入门:从零开始的旅程
【10月更文挑战第23天】本文将带领读者开启一段安卓应用开发的奇妙之旅。我们将从最基础的概念讲起,逐步深入到开发实践,最后通过一个简易的代码示例,展示如何将理论知识转化为实际的应用。无论你是编程新手,还是希望扩展技能的软件工程师,这篇文章都将为你提供有价值的指导和启发。
68 0
安卓开发中的自定义视图:从入门到精通
【9月更文挑战第19天】在安卓开发的广阔天地中,自定义视图是一块充满魔力的土地。它不仅仅是代码的堆砌,更是艺术与科技的完美结合。通过掌握自定义视图,开发者能够打破常规,创造出独一无二的用户界面。本文将带你走进自定义视图的世界,从基础概念到实战应用,一步步展示如何用代码绘出心中的蓝图。无论你是初学者还是有经验的开发者,这篇文章都将为你打开一扇通往创意和效率的大门。让我们一起探索自定义视图的秘密,将你的应用打造成一件艺术品吧!
89 10
Android面试高频知识点(4) 详解Activity的启动流程
讲解Activity的启动流程了,Activity的启动流程相对复杂一下,涉及到了Activity中的生命周期方法,涉及到了Android体系的CS模式,涉及到了Android中进程通讯Binder机制等等, 首先介绍一下Activity,这里引用一下Android guide中对Activity的介绍:
91 4
Android面试之Activity启动流程简述
Android面试之Activity启动流程简述
38 0

热门文章

最新文章

  • 1
    Android历史版本与APK文件结构
    12
  • 2
    【01】噩梦终结flutter配安卓android鸿蒙harmonyOS 以及next调试环境配鸿蒙和ios真机调试环境-flutter项目安卓环境配置-gradle-agp-ndkVersion模拟器运行真机测试环境-本地环境搭建-如何快速搭建android本地运行环境-优雅草卓伊凡-很多人在这步就被难倒了
    21
  • 3
    APP-国内主流安卓商店-应用市场-鸿蒙商店上架之必备前提·全国公安安全信息评估报告如何申请-需要安全评估报告的资料是哪些-优雅草卓伊凡全程操作
    15
  • 4
    当flutter react native 等混开框架-并且用vscode-idea等编译器无法打包apk,打包安卓不成功怎么办-直接用android studio如何打包安卓apk -重要-优雅草卓伊凡
    2
  • 5
    【03】仿站技术之python技术,看完学会再也不用去购买收费工具了-修改整体页面做好安卓下载发给客户-并且开始提交网站公安备案-作为APP下载落地页文娱产品一定要备案-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
    3
  • 6
    【09】flutter首页进行了完善-采用android studio 进行真机调试开发-增加了直播间列表和短视频人物列表-增加了用户中心-卓伊凡换人优雅草Alex-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
    3
  • 7
    【02】仿站技术之python技术,看完学会再也不用去购买收费工具了-本次找了小影-感觉页面很好看-本次是爬取vue需要用到Puppeteer库用node.js扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
    2
  • 8
    Cellebrite UFED 4PC 7.71 (Windows) - Android 和 iOS 移动设备取证软件
    6
  • 9
    escrcpy:【技术党必看】Android开发,Escrcpy 让你无线投屏新体验!图形界面掌控 Android,30-120fps 超流畅!🔥
    3
  • 10
    【01】仿站技术之python技术,看完学会再也不用去购买收费工具了-用python扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-客户的麻将软件需要下载落地页并且要做搜索引擎推广-本文用python语言快速开发爬取落地页下载-优雅草卓伊凡
    1
  • 1
    android FragmentManager 删除所有Fragment 重建
    18
  • 2
    Android实战经验之Kotlin中快速实现MVI架构
    30
  • 3
    即时通讯安全篇(一):正确地理解和使用Android端加密算法
    35
  • 4
    escrcpy:【技术党必看】Android开发,Escrcpy 让你无线投屏新体验!图形界面掌控 Android,30-120fps 超流畅!🔥
    42
  • 5
    【01】噩梦终结flutter配安卓android鸿蒙harmonyOS 以及next调试环境配鸿蒙和ios真机调试环境-flutter项目安卓环境配置-gradle-agp-ndkVersion模拟器运行真机测试环境-本地环境搭建-如何快速搭建android本地运行环境-优雅草卓伊凡-很多人在这步就被难倒了
    142
  • 6
    Cellebrite UFED 4PC 7.71 (Windows) - Android 和 iOS 移动设备取证软件
    46
  • 7
    【03】仿站技术之python技术,看完学会再也不用去购买收费工具了-修改整体页面做好安卓下载发给客户-并且开始提交网站公安备案-作为APP下载落地页文娱产品一定要备案-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
    58
  • 8
    Android历史版本与APK文件结构
    160
  • 9
    【02】仿站技术之python技术,看完学会再也不用去购买收费工具了-本次找了小影-感觉页面很好看-本次是爬取vue需要用到Puppeteer库用node.js扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
    48
  • 10
    【01】仿站技术之python技术,看完学会再也不用去购买收费工具了-用python扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-客户的麻将软件需要下载落地页并且要做搜索引擎推广-本文用python语言快速开发爬取落地页下载-优雅草卓伊凡
    41
  • AI助理

    你好,我是AI助理

    可以解答问题、推荐解决方案等