Android学习路线(十三)Activity生命周期——停止和恢复(Pausing and Resuming )一个Activity

先占个位置,下次翻译~ :p

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.

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.

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.

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

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

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 withinonPause() 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 don’t need to re-initialize components that were created during any of the callback methods leading up to the Resumed state.

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
    }
}

Android学习路线(十三)Activity生命周期——停止和恢复(Pausing and Resuming )一个Activity,布布扣,bubuko.com

时间: 2024-11-19 18:32:08

Android学习路线(十三)Activity生命周期——停止和恢复(Pausing and Resuming )一个Activity的相关文章

Android学习路线(十四)Activity生命周期——停止和重启(Stopping and Restarting)一个Activity

先占个位置,下次翻译~ :p Properly stopping and restarting your activity is an important process in the activity lifecycle that ensures your users perceive that your app is always alive and doesn't lose their progress. There are a few of key scenarios in which

Android学习路线(二十一)运用Fragment构建动态UI——创建一个Fragment

你可以把fragment看成是activity的模块化部分,它拥有自己的生命周期,接受它自己的输入事件,你可以在activity运行时添加或者删除它(有点像是一个"子activity",你可以在不同的activity中重用它).本课将向你展示如何使用Support Libaray继承 Fragment 类来让你的应用能够兼容正在运行Android 1.6的设备. 提示: 如果你决定你的应用需求的最低API级别是11或者更高,那么你不需要使用Support Library,你可以直接使用

android学习笔记28——Activity生命周期

Activity生命周期 Activity的活动状态由android已Activity栈的形式管理,当前活动的Activity位于栈顶.随着不同应用的运行,每个Activity都有可能从活动状态转入非活动状态,从非活动状态转入活动状态. Activity归纳大致会经过4个状态: 1.活动状态:当前Activity位于前台,用户可见,可获得焦点: 2.暂停状态:其他Activity位于前台,该Activity可见,不可获得焦点: 3.停止状态:该Activity不可见,失去焦点: 4.销毁状态:该

Android之Activity生命周期浅析(一)

??Activity作为四大组件之一,出现的频率相当高,基本上我们在android的各个地方都能看见它的踪影,因此深入了解Activity,对于开发高质量应用程序是很有帮助的.今天我们就来详细地聊聊Activity的生命周期,以便我们在以后的开发中能如鱼得水. 一.初识Activity ??在日常应用中Activity是与用户交互的接口,它提供了一个用户完成相关操作的窗口.当我们在开发中创建Activity后,通过调用setContentView(View)方法来给该Activity指定一个布局

浅谈Android之Activity生命周期

 Activity作为四大组件之一,出现的频率相当高,基本上我们在android的各个地方都能看见它的踪影,因此深入了解Activity,对于开发高质量应用程序是很有帮助的.今天我们就来详细地聊聊Activity的生命周期,以便我们在以后的开发中能如鱼得水. 一.初识Activity   在日常应用中Activity是与用户交互的接口,它提供了一个用户完成相关操作的窗口.当我们在开发中创建Activity后,通过调用setContentView(View)方法来给该Activity指定一个布局界

Android Activity生命周期的几个问题

每一个Android开发者都应该知道,android系统有四个重要的基本组件,即Activity(活动).Service(服务).Broadcast Receive(广播接收器)和Content Provider(内容提供器),其中,Activity是最重要的组件,打开你手机上的APP,你眼睛所能看到的都是Activity,下面将会介绍关于Activity生命周期的几个问题. 1 Activity生命周期的8个回调函数 下图是Activity的生命周期图,相信许多人都看过不止一次,但有的人是看到

Android之Activity生命周期详解

Activity的生命周期方法: onCreate()--->onStart()--->onResume()--->onPause()--->onStop()--->onDestory() 单个Activity的三种状态:显示状态,不可见状态,销毁状态.1,activity创建到显示要调用前三个方法.2,点击后退键,做了两件事:(1)当前activity被销毁,调用后面三个周期方法.(2)栈中位于最顶部的Activity显示出来.3,onDestory()方法主要是当Acti

android面试总结01 activity生命周期

面试经常会被问到的: Q:能说一下Activity的生命周期吗? Activity生命周期如下: onCreat onStart onResume onPause onStop onDestory 外加一个 onRestart 就像图示一样,当一个activity第一次启动时会调用的函数分别是 onCreat onStart onResume方法 此时activty会正常运行 . 当出现如图所示,屏幕弹出一个dialog遮住activity,但这是activity还显示一部分时, 会调用onPa

Android学习路线(二十四)ActionBar Fragment运用最佳实践

通过前面的几篇博客,大家看到了Google是如何解释action bar和fragment以及推荐的用法.俗话说没有demo的博客不是好博客,下面我会介绍一下action bar和fragment在实战中的应用,以及相关demo源码,希望和大家相互交流. 了解过fragment的同学们应该都知道,fragment是android 3.0版本才出现的的,因此如果要在支持android 3.0一下版本的工程中使用fragment的话是需要添加Support Library的.具体如何添加我就不再赘述