原创 2011年10月18日 22:50:54


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



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





onPause -> onResume-> onPause




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



图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。



源文档 <>





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




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


源文档 <>



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系统决定
  • 影响应用生命周期的主要因素包括:该进程对于用户的重要性,以及当前系统中还剩多少可用内存。







  • woshimalingyi
  • woshimalingyi
  • 2016年03月23日 13:34
  • 4134


Android中生命周期方法都是由系统调用,系统会根据当前Activity的不同状态回调相应的方法。 1、Activity的生命周期: onCreat():当Activity第一次被创建时回调的方法,...
  • shixhe
  • shixhe
  • 2017年08月10日 21:01
  • 877


  • songshizhuyuan
  • songshizhuyuan
  • 2015年07月29日 17:17
  • 1484

Activity 生命周期 ——各种情况测试

首先上个官方图: 程序两个Activity: ...
  • qiantujava
  • qiantujava
  • 2014年03月21日 14:54
  • 2512


直接上代码吧 MainActivity package test.activitylife; import; import
  • yuxmdef1
  • yuxmdef1
  • 2014年01月09日 11:34
  • 11576


一个Acitivity是一个应用的程序组件,提供一个屏幕,用户可以用来交互完成某些任务.一个应用通常由多个Activity组成. 那么在交互的过程中,必然会存在交互的各种状态,那么这些状态在整个Act...
  • data_hlk
  • data_hlk
  • 2016年05月21日 21:07
  • 2418

Android 菜鸟季:BroadcastReceiver 和 Activity生命周期

两个知识点,因为菜鸟放在了一起。 今天在弄一个电池管理的程序,涉及到建立一个BroadcastReceiver ,建立一个IntentFiler,截取系统广播的Intent.ACTION_BATTE...
  • cowwaka
  • cowwaka
  • 2016年04月11日 18:34
  • 493

Activity 生命周期详细解释

1、不设置Activity的android:configChanges时,切屏会重新调用各个生命周期, 切横屏时会执行一次,切竖屏时会执行两次 2、设置Activity的android:config...
  • qq_34927117
  • qq_34927117
  • 2016年09月01日 22:49
  • 761


  • javazejian
  • javazejian
  • 2016年07月17日 15:31
  • 36573


  • qq_28273051
  • qq_28273051
  • 2016年12月05日 12:34
  • 438