`
weiweirenhong
  • 浏览: 32575 次
  • 性别: Icon_minigender_1
  • 来自: 西安
最近访客 更多访客>>
社区版块
存档分类
最新评论

Application Fundamentals--Component Lifecycles(组件生命周期)

阅读更多
Component Lifecycles--组件生命周期

Application components have a lifecycle — a beginning when Android instantiates them to respond to intents through to an end when the instances are destroyed. In between, they may sometimes be active or inactive,or, in the case of activities, visible to the user or invisible. This section discusses the lifecycles of activities, services, and broadcast receivers — including the states that they can be in during their lifetimes, the methods that notify you of transitions between states, and the effect of those states on the possibility that the process hosting them might be terminated and the instances destroyed.

翻译:组件是有生命周期的,一个组件的生命周期的起点是Android系统为了响应一个intent对象的请求而该组件的一个实例,终点是该组件实例被Android系统销毁。这期间,组件实例有时候是活动状态,有时候是处于睡眠状态,对于Activity组件实例而言所谓活动状态即用户可以看到该 Activity的用户界面,休眠状态就是用户看不到用户界面的时候。这一章节中我们将重点讨论组件生命周期、组件实例在生命周期中的各种可能状态、组件实例状态发生变换时将要触发的不同函数以及不同状态导致宿主进程终止并销毁该组件实例的可能性。
Activity lifecycle--Activity组件的生命周期

An activity has essentially three states:翻译:Activity组件有三种状态

    * It is active or running when it is in the foreground of the screen (at the top of the activity stack for the current task). This is the activity that is the focus for the user's actions.【翻译:活动状态或称之为运行状态,这时候Activity实例的界面显示在当前屏幕上,此时这个Activity实例在当前task堆栈中处于栈顶位置。】
    *

      It is paused if it has lost focus but is still visible to the user. That is, another activity lies on top of it and that activity either is transparent or doesn't cover the full screen, so some of the paused activity can show through. A paused activity is completely alive (it maintains all state and member information and remains attached to the window manager), but can be killed by the system in extreme low memory situations.【翻译:暂停状态,暂停状态的Activity实例失去了用户焦点,但是依然是用户可见状态,此时堆栈中已有了另一个Activity实例处于这个实例的上面,但是新的实例的用户界面还没有占据当前屏幕,暂停状态的 Activity依然持有所有自身状态信息并且与窗体管理对象依然保持关联,但是如果说系统出现内存极端不足的话,系统是可以销毁该实例的。】
    *

      It is stopped if it is completely obscured by another activity. It still retains all state and member information. However, it is no longer visible to the user so its window is hidden and it will often be killed by the system when memory is needed elsewhere.【翻译:停止状态,停止状态的activity实例的用户界面已经完全被另外一个实例的界面所掩盖,但它依然持有所有的状态信息,不过,对于用户来说已经是不可见的了。如果说系统出现内存不足的话,系统是可以销毁该实例的。】

If an activity is paused or stopped, the system can drop it from memory either by asking it to finish (calling its finish() method), or simply killing its process. When it is displayed again to the user, it must be completely restarted and restored to its previous state.

翻译:一旦一个activity的状态是暂停状态或是停止状态,系统就可以调用该实例的finish()方法以便从内存中销毁该实例,或者是直接杀死该实例的进程。之后如果该activity实例需要重新显示在屏幕上,必须彻底重新启动实例并恢复实例之前的状态。

As an activity transitions from state to state, it is notified of the change by calls to the following protected methods(翻译:activity的状态发生变化的时候,以下方法(七个)将被系统调用:):

void onCreate(Bundle savedInstanceState)
void onStart()
void onRestart()
void onResume()
void onPause()
void onStop()
void onDestroy()


All of these methods are hooks that you can override to do appropriate work when the state changes. All activities must implement onCreate() to do the initial setup when the object is first instantiated. Many will also implement onPause() to commit data changes and otherwise prepare to stop interacting with the user.

翻译:这些方法可以被开发者override,以便当状态变化时执行相应的操作。所有的activity类都必须实现onCreate()方法,这个方法是实例的初始化方法,很多activity同时还实现了onPause() 方法,实现这个方法的目的是及时提交最新数据或者是为停止与用户交互做必要的准备(因为某个activity实例的状态切换到暂停状态的时候,系统将调用该实例的onPause()方法,换句话说activity实例在暂停与用户交互前,该方法将被系统调用执行。)。

Calling into the superclass

An implementation of any activity lifecycle method should always first call the superclass version. For example:【翻译:实现activity的任何生命周期方法的时候都必须首先调用父类的方法,例如:】

protected void onPause() {
    super.onPause();
    . . .
}


Taken together, these seven methods define the entire lifecycle of an activity. There are three nested loops that you can monitor by implementing them:

翻译:Activity整个生命周期中一共定义了七个方法,Activity整个生命周期中有三个内部状态循环圈,实现这七个方法可以监视这三个内部状态循环圈。

    * The entire lifetime of an activity happens between the first call to onCreate() through to a single final call to onDestroy(). An activity does all its initial setup of "global" state in onCreate(), and releases all remaining resources in onDestroy(). For example, if it has a thread running in the background to download data from the network, it may create that thread in onCreate() and then stop the thread in onDestroy().

      翻译:1、整个生命周期 :activity的生命周期起于onCreate()方法被调用(建立初始状态),终止于onDestroy()被调用(释放持有的资源),例如:如果一个activity需要在另外一个线程中在后台下载网络数据,可以在 onCreate()方法中创建一个线程执行下载网络数据的操作,在onDestroy()方法中应该终止该线程。
    *

      The visible lifetime of an activity happens between a call to onStart() until a corresponding call to onStop(). During this time, the user can see the activity on-screen, though it may not be in the foreground and interacting with the user. Between these two methods, you can maintain resources that are needed to show the activity to the user. For example, you can register a BroadcastReceiver in onStart() to monitor for changes that impact your UI, and unregister it in onStop() when the user can no longer see what you are displaying. The onStart() and onStop() methods can be called multiple times, as the activity alternates between being visible and hidden to the user.

      翻译:2、可视生命周期:activity的可视生命周期起于onStart()方法被调用,终止于onStop()方法被调用。整个可视生命周期中,activity的界面在屏幕上是用户可见的,此时这个activity也许不在前台与用户发生交互。例如,你可以在onStart()方法中注册一个BroadcastReceiver用来监视当前界面的变化,在onStop()方法中注销曾经注册的那个BroadcastReceiver,因为这时候用户已经看不到这个组件的界面了。这两个方法可能被系统多次调用,这是因为activity的UI界面对于用户来说经常是时而可见,时而不可见。
    *

      The foreground lifetime of an activity happens between a call to onResume() until a corresponding call to onPause(). During this time, the activity is in front of all other activities on screen and is interacting with the user. An activity can frequently transition between the resumed and paused states — for example, onPause() is called when the device goes to sleep or when a new activity is started, onResume() is called when an activity result or a new intent is delivered. Therefore, the code in these two methods should be fairly lightweight.

      翻译:3、前台生命周期:activity的前台生命周期起于onResume()方法被调用,终止于 onPause()方法被调用。整个前台生命周期中,此时的activity处于其他activity之前(task堆栈栈顶元素的角色)、显示在当前屏幕上与用户做交互,activity实例可以重复在继续和暂停状态之间切换,例如在手机进入休眠状态的时候或是一个新的activity实例被启动的时候,onPause()方法就被系统调用执行,onResume()方法被调用的时候是当这个activity期望的结果被返回的时候或是当一个新的intent请求对象发送给这个activity实例的时候。所以,通常这两个方法中的代码应该是很简洁的。

The following diagram illustrates these loops and the paths an activity may take between states. The colored ovals are major states the activity can be in. The square rectangles represent the callback methods you can implement to perform operations when the activity transitions between states.【翻译:下图显示了activity生命周期中的状态变换过程,带颜色的椭圆形表示activity状态,方框是发生状态变化时的回调方法。】















Note the Killable column in the table above. It indicates whether or not the system can kill the process hosting the activity at any time after the method returns, without executing another line of the activity's code. Three methods (onPause(), onStop(), and onDestroy()) are marked "Yes." Because onPause() is the first of the three, it's the only one that's guaranteed to be called before the process is killed — onStop() and onDestroy() may not be. Therefore, you should use onPause() to write any persistent data (such as user edits) to storage.

翻译:要注意的是 是否实例所在的进程可以被系统杀死,onPause(), onStop(),和 onDestroy()三个方法在这点上是有区别的,系统要杀死实例所在的进程之前,实例的onPause()方法一定是会被系统调用的,但是其他两个方法不保证一定被系统调用,所以,如果需要及时保存数据,避免数据丢失,一定是在onPause()方法中做数据保存操作。

Methods that are marked "No" in the Killable column protect the process hosting the activity from being killed from the moment they are called. Thus an activity is in a killable state, for example, from the time onPause() returns to the time onResume() is called. It will not again be killable until onPause() again returns.

As noted in a later section, Processes and lifecycle, an activity that's not technically "killable" by this definition might still be killed by the system — but that would happen only in extreme and dire circumstances when there is no other recourse.
  • 大小: 71.2 KB
  • 大小: 31.3 KB
  • 大小: 18.4 KB
  • 大小: 23.1 KB
  • 大小: 19.4 KB
  • 大小: 19 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics