首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

小弟我对Activity Lifecycle的理解

2012-09-08 
我对Activity Lifecycle的理解本文部分来自android-developer-dev guide-Application Fundamentals第一

我对Activity Lifecycle的理解
本文部分来自android->developer->dev guide->Application Fundamentals

第一部分 activity stack
Activities in the system are managed as an activity stack. When a new activity is started, it is placed on the top of the stack and becomes the running activity -- the previous activity always remains below it in the stack, and will not come to the foreground again until the new activity exits.

注释:重点理解,Activity是被一个activity stack管理的。

第二部分 essentially three states
An activity has essentially three states:

    * 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.
    * 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.
    * 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 的三个重要的状态:active or running, paused, stopped.

第三部分 three nested loops
There are three nested loops about 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().
    * 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.
    * 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.

注释:
entire lifetime, 从onCreate()->onDestroy(),例子,thread后台下载数据;
visible lifetime,从onStart()->onStop(),例子,可见的状态,虽然不与前台用户互动;
foreground lifetime,从onResume()->onPause(),例子,在activity运行期间,被一个新的activity打断.


第四部分 diagram illustrates
有了以上的理解,对于activity生命周期的图来说,自然而然就熟记忆了。


热点排行