Activity的生命周期

Activity从建立到销毁的过程中需要在不同的阶段调用7个生命周期方法。这7个生命周期方法的定义如下:

  1. protected void onCreate(Bundle savedInstanceState)
  2. protected void onStart()
  3. protected void onResume()
  4. protected void onPause()
  5. protected void onStop()
  6. protected void onRestart()
  7. protected void onDestroy()

 

上面7个生命周期方法分别在4个阶段按着一定的顺序进行调用,这4个阶段如下:

  • 开始Activity:在这个阶段依次执行3个生命周期方法:onCreate、onStart和onResume。
  • Activity失去焦点:如果在Activity获得焦点的情况下进入其他的Activity或应用程序,这时当前的Activity会失去焦点。在这一阶段,会依次执行onPause和onStop方法。
  • Activity重新获得焦点:如果Activity重新获得焦点,会依次执行3个生命周期方法:onRestart、onStart和onResume。
  • 关闭Activity:当Activity被关闭时系统会依次执行3个生命周期方法:onPause、onStop和onDestroy。

 

如果在这4个阶段执行生命周期方法的过程中不发生状态的改变,那么系统会按着上面的描述依次执行这4个阶段中的生命周期方法,但如果在执行的过程中改变了状态,系统会按着更复杂的方式调用生命周期方法。

在执行的过程中可以改变系统的执行轨迹的生命周期方法是onPauseonStop。如果在执行onPause方法的过程中Activity重新获得了焦点,然后又失去了焦点。系统将不会再执行onStop方法,而是按着如下的顺序执行相应的生命周期方法:

 

onPause -> onResume-> onPause

 

如果在执行onStop方法的过程中Activity重新获得了焦点,然后又失去了焦点。系统将不会执行onDestroy方法,而是按着如下的顺序执行相应的生命周期方法:

 

onStop -> onRestart -> onStart -> onResume -> onPause -> onStop

 

1详细描述了这一过程。

图1  Activity的生命周期

 

从图1所示的Activity生命周期不难看出,在这个图中包含了两层循环,第一层循环是onPause-> onResume -> onPause,第二层循环是onStop -> onRestart -> onStart -> onResume -> onPause-> onStop。我们可以将这两层循环看成是整合Activity生命周期中的子生命周期。第一层循环称为焦点生命周期,第二层循环称为可视生命周期。也就是说,第一层循环在Activity焦点的获得与失去的过程中循环,在这一过程中,Activity始终是可见的。而第二层循环是在Activity可见与不可见的过程中循环,在这个过程中伴随着Activity的焦点的获得与失去。也就是说,Activity首先会被显示,然后会获得焦点,接着失去焦点,最后由于弹出其他的Activity,使当前的Activity变成不可见。因此,Activity有如下3种生命周期:

  • 整体生命周期:onCreate -> ... ... -> onDestroy。
  • 可视生命周期:onStop -> ... ... -> onPause。
  • 焦点生命周期:onPause -> onResume。

注意:在图1所示的Activity生命周期里可以看出,系统在终止应用程序进程时会调用onPauseonStoponDesktroy方法。而onPause方法排在了最前面,也就是说,Activity在失去焦点时就可能被终止进程,而onStoponDestroy方法可能没有机会执行。因此,应该在onPause方法中保存当前Activity状态,这样才能保证在任何时候终止进程时都可以执行保存Activity状态的代码。

 

源文档 <http://dev.10086.cn/cmdn/bbs/viewthread.php?tid=14414&highlight=>

 

 

Activity栈:

Android通过Activity栈的方式来管理Activity。

正在运行的Activity 处在在栈的最顶端,它是运行状态的;

当有新Activity进入屏幕最上端时,原来的Activity就会被压入第二层,如果他的屏幕没有被完全遮盖,那么他处于Pause状态,如果他被遮盖那么他处于Stop状态。

 

当然不管你出于任何一层,都可能在系统觉得资源不足时被强行关闭,当然关闭时在栈底的程序最先被关闭。

譬如:当你在程序中调用 Activity.finish()方法时,结果和用户按下 BACK 键一样:他告诉 Activity Manager该Activity实例可以被“回收”。随后 Activity Manager 激活处于栈第二层的 Activity 并重新入栈,把原 Activity 压入到栈的第二层,从 Running 状态转到 Paused 状态。

 

源文档 <http://android.yaohuiji.com/archives/141>

 

 

Shutting down components

 

A content provider is active only while it's responding to a request from a ContentResolver. And a broadcast receiver is active only while it's responding to a broadcast message. So there's no need to explicitly shut down these components.

Activities, on the other hand, provide the user interface. They're in a long-running conversation with the user and may remain active, even when idle, as long as the conversation continues. Similarly, services may also remain running for a long time. So Android has methods to shut down activities and services in an orderly way:

Components might also be shut down by the system when they are no longer being used or when Android must reclaim memory for more active components. A later section,Component Lifecycles, discusses this possibility and its ramifications in more detail.

 

Why handling life-cycle events is important

If you are used to always having control in your applications, you might not understand why all this life-cycle work is necessary. The reason is that in Android, you are not in control of your Activity, the operating system is!

 

As we have already seen, the Android model is based around activities calling each other. When one Activity calls another, the current Activity is paused at the very least, and may be killed altogether if the system starts to run low on resources. If this happens, your Activity will have to store enough state to come back up later, preferably in the same state it was in when it was killed.

 

onSaveInstanceState() is called by Android if the Activity is being stopped andmay be killed before it is resumed! This means it should store any state necessary to re-initialize to the same condition when the Activity is restarted. It is the counterpart to theonCreate() method, and in fact the savedInstanceState Bundle passed in to onCreate() is the same Bundle that you construct as outState in theonSaveInstanceState() method.

 

onPause() andonResume() are also complimentary methods. onPause() is always called when the Activity ends, even if we instigated that (with a finish() call for example). We will use this to save the current note back to the database. Good practice is to release any resources that can be released during an onPause() as well, to take up less resources when in the passive state.onResume() will call our populateFields() method to read the note out of the database again and populate the fields.

 

 

In Android, an application can be “alive” even if its process has beenkilled. Put another way, the activity life cycle is not tied to the processlife cycle. Processes are just disposablecontainers for activities.

 

 

You override these methods in your Activity class, and Android will call

them at the appropriate time:

 

• onCreate(Bundle): This is called when the activity first starts up.You can use it to perform one-time initialization such as creatingthe user interface. onCreate( ) takes one parameter that is eithernull or some state information previously saved by the onSaveInstanceState(

) method.

 

• onStart( ): This indicates the activity is about to be displayed to theuser.

 

• onResume( ): This is called when your activity can start interactingwith the user. This is a good place to start animations and music.

 

• onPause( ): This runs when the activity is about to go into the background,usually because another activity has been launched infront of it. This is where you should save your program’s persistentstate, such as a database record being edited.

 

• onStop( ): This is called when your activity is no longer visible tothe user and it won’t be needed for a while. If memory is tight,onStop( ) may never be called (the system may simply terminateyour process).

 

• onRestart( ): If this method is called, it indicates your activity isbeing redisplayed to the user from a stopped state.

 

• onDestroy( ): This is called right before your activity is destroyed. Ifmemory is tight, onDestroy( ) may never be called (the system maysimply terminate your process).

 

• onSaveInstanceState(Bundle): Android will call this method to allowthe activity to save per-instance state, such as a cursor positionwithin a text field. Usually you won’t need to override it becausethe default implementation saves the state for all your user interface

controls automatically.

 

• onRestoreInstanceState(Bundle): This is called when the activity isbeing reinitialized from a state previously saved by the onSave-InstanceState( ) method. The default implementation restores thestate of your user interface.

 

 

  • Android引入了一个全新的机制-应用程序生命周期(Life Cycle)。
  • 多数情况下,一个Android应用运行在一个独立的Linux进程中
  • 应用进程的的生命周期(存活时间)不是由进程自己控制,而是由Android系统决定
  • 影响应用生命周期的主要因素包括:该进程对于用户的重要性,以及当前系统中还剩多少可用内存。

 

 

为了决定在内存不足情况下消毁哪个进程,Android会根据这些进程内运行的组件及这些组件的状态,把这些进程划分出一个“重要性层次”。这个层次按顺序如下:
       1、前端进程是拥有一个显示在屏幕最前端并与使用者做交互的Activity(它的onResume已被调用)的进程,也可能是一个拥有正在运行的IntentReceiver(它的onReceiveIntent()方法正在运行)的进程。在系统中,这种进程是很少的,只有当内存低到不足于支持这些进程的继续运行,才会将这些进程消毁。通常这时候,设备已经达到了需要进行内存整理的状态,为了保障用户界面不停止响应,只能消毁这些进程;
      2、可视进程是拥有一个用户在屏幕上可见的,但并没有在前端显示的Activity(它的onPause已被调用)的进程。例如:一个以对话框显示的前端activity在屏幕上显示,而它后面的上一级activity仍然是可见的。这样的进程是非常重要的,一般不会被消毁,除非为了保障所有的前端进程正常运行,才会被消毁。
       3、服务进程是拥有一个由startService()方法启动的Service的进程。尽管这些进程对于使用者是不可见的,但他们做的通常是使用者所关注的事情(如后台MP3播放器或后台上传下载数据的网络服务)。因此,除非为了保障前端进程和可视进程的正常运行,系统才会消毁这种进程。
      4、后台进程是拥有一个用户不可见的ActivityonStop()方法已经被调用)的进程。这些进程不直接影响用户的体验。如果这些进程正确地完成了自己的生命周期(详细参考Activity类),系统会为了以上三种类型进程,而随时消毁这种进程以释放内存。通常会有很多这样的进程在运行着,因些这些进程会被保存在一个LRU列表中,以保证在内存不足时,用户最后看到的进程将在最后才被消毁。
      5、空进程是那些不拥有任何活动的应用组件的进程。保留这些进程的唯一理由是,做为一个缓存,在它所属的应用的组件下一次需要时,缩短启动的时间。同样的,为了在这些缓存的空进程和底层的核心缓存之间平衡系统资源,系统会经常消毁这些空进程。
      当要对一个进程进行分类时,系统会选择在这个进程中所有活动的组件中重要等级最高的那个做为依据。可以参考ActivityServiceIntentReceiver文档,了解这些组件如何影响进程整个生命周期的更多细节。这些类的文档都对他们如何影响他们所属的应用的整个生命周期,做了详细的描述。

 

 

阅读更多
个人分类: Android 基础开发
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭