Activity的生命周期
onPause()<wbr style="line-height:25px">:activity仍然可见但是已经不在最前端。<br style="line-height:25px"><a target="_blank" rel="nofollow" href="http://developer.android.com/reference/android/app/Activity.html#onStop%28%29" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">onStop()</a><wbr style="line-height:25px">:activity不可以见。如果在该阶段因为内存原因被杀死。可通过onSaveInstanceState(Bundle)保存的Bundle数据重新创建(onCreate)<br style="line-height:25px"><a target="_blank" rel="nofollow" href="http://developer.android.com/reference/android/app/Activity.html#onDestroy%28%29" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">onDestroy()</a><wbr style="line-height:25px">:activity被销毁。<br style="line-height:25px"> 比如调用了finish()系列的函数,按了back键,因为内存原因,Activity被杀死时,及ConfigurationChanges后。<br style="line-height:25px"> 更加详细请参考《<strong><a title="阅读全文" target="_blank" href="http://hubingforever.blog.163.com/blog/static/171040579201062323127905/" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">Activity重生机制</a></strong>》<br style="line-height:25px"> 在Activity的API中有大量的onXXXX形式的函数定义,除了我们前面用到的onCreate以外,<br style="line-height:25px"> 还有onStart,onStop以及onPause等等。从字面上看,它们是一些事件回调,那么次序又是如何的呢?<br style="line-height:25px"> 其实这种事情,自己做个实验最明白不过了。在做这个实验之前,我们先得找到在Android中的Log是如何输出的。<br style="line-height:25px"> 好了,现在我们修改前面的Hello1代码:<br style="line-height:25px"><span style="color:#3366ff; line-height:25px">finalStringTAG="hubin";<br style="line-height:25px"> finalStringTAG_SUB="one";<br style="line-height:25px"></span><span style="color:#993300; line-height:25px">publicvoid</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff6600; line-height:25px">onStart()</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">super.onStart();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Log.v(TAG,TAG_SUB+"onStart");</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">publicvoid</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff6600; line-height:25px">onStop()</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">super.onStop();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Log.v(TAG,TAG_SUB+"onStop");</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">publicvoid</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff6600; line-height:25px">onResume()</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">super.onResume();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Log.v(TAG,TAG_SUB+"onResume");</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">publicvoid</span><span style="color:#ff6600; line-height:25px">onRestart()</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">super.onRestart();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Log.v(TAG,TAG_SUB+"onReStart");</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">publicvoid</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff6600; line-height:25px">onPause()</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">super.onPause();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Log.v(TAG,TAG_SUB+"onPause");</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">publicvoid</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff6600; line-height:25px">onDestroy()</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">super.onDestroy();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Log.v(TAG,TAG_SUB+"onDestroy");</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">publicvoid</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff6600; line-height:25px">onSaveInstanceState(BundleoutState)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">super.onSaveInstanceState(outState);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Log.v(TAG,TAG_SUB+"onSaveInstanceState");</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"> 在Hello2中也同样增加这样的代码,编译,运行一下,从logcat中分析输出的日志。<br style="line-height:25px"> 在启动第一个界面ActivityOne时,它的次序是:<br style="line-height:25px"><span style="color:#ff6600; line-height:25px">onCreate(ONE)</span>-><span style="color:#ff6600; line-height:25px">onStart(ONE)</span>-><span style="color:#ff6600; line-height:25px">onResume(ONE)</span><br style="line-height:25px"><span style="color:#000080; line-height:25px">虽然是第一次启动,也要走一遍这个resume事件</span>。然后,我们点goto跳到第二个ActivityTwo中(前一个没有关闭),这时走的次序是:<br style="line-height:25px"><span style="color:#ff6600; line-height:25px">onSaveInstanceState(ONE)</span>-><span style="color:#ff6600; line-height:25px">onPause(ONE)</span>-><span style="color:#ff6600; line-height:25px">onCreate(TWO)</span>-<span style="color:#ff6600; line-height:25px">onStart(TWO)</span>-<span style="color:#ff6600; line-height:25px">onResume(TWO)</span>-<span style="color:#ff6600; line-height:25px">onStop(ONE)</span><br style="line-height:25px"><span style="color:#000080; line-height:25px">说明,第二个ActivityTwo在启动前,One会经历一个:冻结、暂停的过程,在启动Two后,One才会被停止?</span><br style="line-height:25px"> 然后,我们再点back回到第一个界面,这时走的次序是:<br style="line-height:25px"><span style="color:#993300; line-height:25px">onPause(TWO)</span>-<span style="color:#993300; line-height:25px">onActivityResult(ONE)</span>-<span style="color:#993300; line-height:25px">onStart(ONE)</span>-<span style="color:#993300; line-height:25px">onRestart(ONE)</span>-<span style="color:#993300; line-height:25px">onResume(ONE)</span>-<span style="color:#993300; line-height:25px">onStop(TWO)</span>-<span style="color:#993300; line-height:25px">onDestroy(TWO)</span><br style="line-height:25px"> 说明,返回时,Two没有经历onSaveInstanceState就直接暂停了,在One接收参数,重启后,Two就停止并被销毁了。<br style="line-height:25px"> 最后,我们点一下Exit退出应用,它的次序是:<br style="line-height:25px"><span style="color:#993300; line-height:25px">onPause(ONE)</span>-<span style="color:#993300; line-height:25px">onStop(ONE)</span>-<span style="color:#993300; line-height:25px">onDestroy(ONE)</span><br style="line-height:25px"> 说明如果我们用了finish的话,不会有onSaveInstanceState,但是仍会经历pause-stop才被销毁。<br style="line-height:25px"><br style="line-height:25px"> 这里有点疑问的是:为什么回来时先是Start才是Restart?可是文档中的图上画的却是先restart再start的啊?不过,后面的表格中的描述好象是正确的,start后面总是跟着resume(如果是第一次)或者restart(如果原来被stop掉了,这种情况会在start与resume中插一个restart)。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意:</wbr></span><wbr style="line-height:25px"><br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">参照文档如果onSaveInstanceState要被调用的话,只能保证它在stop之前调用。但它可以在onPause()之前OR之后调用</wbr></span><wbr style="line-height:25px">。<br style="line-height:25px"> 下面不跑例子了,看看文档吧。<br style="line-height:25px"><br style="line-height:25px"><span style="line-height:25px">1.</span><span style="color:#000080; line-height:25px">Android用ActivityStack来管理多个Activity,所以呢,同一时刻只会有最顶上的那个Activity是处于active或者running状态。其它的Activity都被压在下面了。</span><br style="line-height:25px"><span style="line-height:25px">2.</span><span style="color:#000080; line-height:25px">如果非活动的Activity仍是可见的(即如果上面压着的是一个非全屏的Activity或透明的Activity),它是处于paused状态的。在系统内存不足的情况下,paused状态的Activity是有可被系统杀掉的。</span><br style="line-height:25px"><span style="line-height:25px">3.</span><span style="color:#000080; line-height:25px">几个事件的配对可以比较清楚地理解它们的关系。</span><span style="color:#993300; line-height:25px">Create</span><span style="color:#000080; line-height:25px">与</span><span style="color:#993300; line-height:25px">Destroy</span><span style="color:#000080; line-height:25px">配成一对,叫entrielifetime,在创建时分配资源,则在销毁时释放资源;往上一点还有</span><span style="color:#993300; line-height:25px">Start</span><span style="color:#000080; line-height:25px">与</span><span style="color:#993300; line-height:25px">Stop</span><span style="color:#000080; line-height:25px">一对,叫visiblelifetime,表达的是可见与非可见这么一个过程;最顶上的就是</span><span style="color:#993300; line-height:25px">Resume</span><span style="color:#000080; line-height:25px">和</span><span style="color:#993300; line-height:25px">Pause</span><span style="color:#000080; line-height:25px">这一对了,叫foregroundlifetime,表达的了是否处于激活状态的过程。</span><br style="line-height:25px"><span style="line-height:25px">4.</span>我们实现的Activity派生类的时候往往要重载两个重要的方法:<span style="color:#000080; line-height:25px">onCreate()进行初始化操作,onPause()保存当前操作的结果。</span><br style="line-height:25px"><br style="line-height:25px"> 除了ActivityLifecycle以外,Android还有一个ProcessLifecycle的说明:<br style="line-height:25px"><span style="color:#003366; line-height:25px">在内存不足的时候,Android是会主动清理门户的,那它又是如何判断哪个process是可以清掉的呢?文档中也提到了它的重要性排序:<br style="line-height:25px"> ClassOverview(android.app.Activity的文档中的类描述)</span><br style="line-height:25px"><span style="line-height:25px">1</span>.<span style="color:#000080; line-height:25px">最容易被清掉的是</span><span style="color:#993300; line-height:25px">emptyprocess</span><span style="color:#000080; line-height:25px">,空进程是指那些没有Activity与之绑定,也没有任何应用程序组件(如Services或者IntentReceiver)与之绑定的进程,也就是说在这个process中没有任何activity或者service之类的东西,它们仅仅是作为一个cache,在启动新的Activity时可以提高速度。它们是会被优先清掉的。因此建议,我们的后台操作,最好是作成Service的形式,也就是说应该在Activity中启动一个Service去执行这些操作</span>。<br style="line-height:25px"><span style="line-height:25px">2.</span><span style="color:#000080; line-height:25px">接下来就是</span><span style="color:#993300; line-height:25px">backgroundactivity</span><span style="color:#000080; line-height:25px">了,也就是被stop掉了那些activity所处的process,那些不可见的Activity被清掉的确是安全的,系统维持着一个LRU列表,多个处于background的activity都在这里面,系统可以根据LRU列表判断哪些activity是可以被清掉的,以及其中哪一个应该是最先被清掉。不过,文档中提到在这个已被清掉的Activity又被重新创建的时候,它的onCreate会被调用,参数就是onSaveInstanceState时的那个Bundle。【Activity被killed时,Android会帮它保留着这个Bundle。】</span><br style="line-height:25px"><span style="line-height:25px">3.</span><span style="color:#000080; line-height:25px">然后就轮到</span><span style="color:#993300; line-height:25px">serviceprocess</span><span style="color:#000080; line-height:25px">了,这是一个与Service绑定的进程,由startService方法启动。虽然它们不为用户所见,但一般是在处理一些长时间的操作(例如MP3的播放),系统会保护它,除非真的没有内存可用了。</span><br style="line-height:25px"><span style="line-height:25px">4.</span><span style="color:#000080; line-height:25px">接着又轮到那些</span><span style="color:#993300; line-height:25px">可见但不是在前台的activity</span><span style="color:#000080; line-height:25px">了,或者说visibleprocess。前面也谈到这个情况,被Paused的Activity也是有可能会被系统清掉,不过相对来说,它已经是处于一个比较安全的位置了。</span><br style="line-height:25px"><span style="line-height:25px">5.</span><span style="color:#000080; line-height:25px">最安全应该就是那个</span><span style="color:#993300; line-height:25px">foregroundactivity</span><span style="color:#000080; line-height:25px">了,不到迫不得已它是不会被清掉的。这种process不仅包括resume之后的activity,也包括那些onReceiveIntent之后的IntentReceiver实例。</span><br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意:所谓killactivity或killService是指kill它们所在的进程。<br style="line-height:25px"></wbr></span><wbr style="line-height:25px">在AndroidApplication的生命周期的讨论中,文档也提到了一些需要注意的事项:因为Android应用程序的生存期并不是由应用本身直接控制的,而是由Android系统平台进行管理的,所以,对于我们开发者而言,需要了解不同的组件Activity、Service和IntentReceiver的生命,切记的是:如果组件的选择不当,很有可能系统会杀掉一个正在进行重要工作的进程。</wbr></wbr></wbr></wbr></wbr></wbr>
onPause()<wbr style="line-height:25px">:activity仍然可见但是已经不在最前端。<br style="line-height:25px"><a target="_blank" rel="nofollow" href="http://developer.android.com/reference/android/app/Activity.html#onStop%28%29" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">onStop()</a><wbr style="line-height:25px">:activity不可以见。如果在该阶段因为内存原因被杀死。可通过onSaveInstanceState(Bundle)保存的Bundle数据重新创建(onCreate)<br style="line-height:25px"><a target="_blank" rel="nofollow" href="http://developer.android.com/reference/android/app/Activity.html#onDestroy%28%29" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">onDestroy()</a><wbr style="line-height:25px">:activity被销毁。<br style="line-height:25px"> 比如调用了finish()系列的函数,按了back键,因为内存原因,Activity被杀死时,及ConfigurationChanges后。<br style="line-height:25px"> 更加详细请参考《<strong><a title="阅读全文" target="_blank" href="http://hubingforever.blog.163.com/blog/static/171040579201062323127905/" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">Activity重生机制</a></strong>》<br style="line-height:25px"> 在Activity的API中有大量的onXXXX形式的函数定义,除了我们前面用到的onCreate以外,<br style="line-height:25px"> 还有onStart,onStop以及onPause等等。从字面上看,它们是一些事件回调,那么次序又是如何的呢?<br style="line-height:25px"> 其实这种事情,自己做个实验最明白不过了。在做这个实验之前,我们先得找到在Android中的Log是如何输出的。<br style="line-height:25px"> 好了,现在我们修改前面的Hello1代码:<br style="line-height:25px"><span style="color:#3366ff; line-height:25px">finalStringTAG="hubin";<br style="line-height:25px"> finalStringTAG_SUB="one";<br style="line-height:25px"></span><span style="color:#993300; line-height:25px">publicvoid</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff6600; line-height:25px">onStart()</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">super.onStart();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Log.v(TAG,TAG_SUB+"onStart");</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">publicvoid</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff6600; line-height:25px">onStop()</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">super.onStop();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Log.v(TAG,TAG_SUB+"onStop");</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">publicvoid</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff6600; line-height:25px">onResume()</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">super.onResume();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Log.v(TAG,TAG_SUB+"onResume");</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">publicvoid</span><span style="color:#ff6600; line-height:25px">onRestart()</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">super.onRestart();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Log.v(TAG,TAG_SUB+"onReStart");</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">publicvoid</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff6600; line-height:25px">onPause()</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">super.onPause();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Log.v(TAG,TAG_SUB+"onPause");</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">publicvoid</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff6600; line-height:25px">onDestroy()</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">super.onDestroy();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Log.v(TAG,TAG_SUB+"onDestroy");</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">publicvoid</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff6600; line-height:25px">onSaveInstanceState(BundleoutState)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">super.onSaveInstanceState(outState);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Log.v(TAG,TAG_SUB+"onSaveInstanceState");</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"> 在Hello2中也同样增加这样的代码,编译,运行一下,从logcat中分析输出的日志。<br style="line-height:25px"> 在启动第一个界面ActivityOne时,它的次序是:<br style="line-height:25px"><span style="color:#ff6600; line-height:25px">onCreate(ONE)</span>-><span style="color:#ff6600; line-height:25px">onStart(ONE)</span>-><span style="color:#ff6600; line-height:25px">onResume(ONE)</span><br style="line-height:25px"><span style="color:#000080; line-height:25px">虽然是第一次启动,也要走一遍这个resume事件</span>。然后,我们点goto跳到第二个ActivityTwo中(前一个没有关闭),这时走的次序是:<br style="line-height:25px"><span style="color:#ff6600; line-height:25px">onSaveInstanceState(ONE)</span>-><span style="color:#ff6600; line-height:25px">onPause(ONE)</span>-><span style="color:#ff6600; line-height:25px">onCreate(TWO)</span>-<span style="color:#ff6600; line-height:25px">onStart(TWO)</span>-<span style="color:#ff6600; line-height:25px">onResume(TWO)</span>-<span style="color:#ff6600; line-height:25px">onStop(ONE)</span><br style="line-height:25px"><span style="color:#000080; line-height:25px">说明,第二个ActivityTwo在启动前,One会经历一个:冻结、暂停的过程,在启动Two后,One才会被停止?</span><br style="line-height:25px"> 然后,我们再点back回到第一个界面,这时走的次序是:<br style="line-height:25px"><span style="color:#993300; line-height:25px">onPause(TWO)</span>-<span style="color:#993300; line-height:25px">onActivityResult(ONE)</span>-<span style="color:#993300; line-height:25px">onStart(ONE)</span>-<span style="color:#993300; line-height:25px">onRestart(ONE)</span>-<span style="color:#993300; line-height:25px">onResume(ONE)</span>-<span style="color:#993300; line-height:25px">onStop(TWO)</span>-<span style="color:#993300; line-height:25px">onDestroy(TWO)</span><br style="line-height:25px"> 说明,返回时,Two没有经历onSaveInstanceState就直接暂停了,在One接收参数,重启后,Two就停止并被销毁了。<br style="line-height:25px"> 最后,我们点一下Exit退出应用,它的次序是:<br style="line-height:25px"><span style="color:#993300; line-height:25px">onPause(ONE)</span>-<span style="color:#993300; line-height:25px">onStop(ONE)</span>-<span style="color:#993300; line-height:25px">onDestroy(ONE)</span><br style="line-height:25px"> 说明如果我们用了finish的话,不会有onSaveInstanceState,但是仍会经历pause-stop才被销毁。<br style="line-height:25px"><br style="line-height:25px"> 这里有点疑问的是:为什么回来时先是Start才是Restart?可是文档中的图上画的却是先restart再start的啊?不过,后面的表格中的描述好象是正确的,start后面总是跟着resume(如果是第一次)或者restart(如果原来被stop掉了,这种情况会在start与resume中插一个restart)。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意:</wbr></span><wbr style="line-height:25px"><br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">参照文档如果onSaveInstanceState要被调用的话,只能保证它在stop之前调用。但它可以在onPause()之前OR之后调用</wbr></span><wbr style="line-height:25px">。<br style="line-height:25px"> 下面不跑例子了,看看文档吧。<br style="line-height:25px"><br style="line-height:25px"><span style="line-height:25px">1.</span><span style="color:#000080; line-height:25px">Android用ActivityStack来管理多个Activity,所以呢,同一时刻只会有最顶上的那个Activity是处于active或者running状态。其它的Activity都被压在下面了。</span><br style="line-height:25px"><span style="line-height:25px">2.</span><span style="color:#000080; line-height:25px">如果非活动的Activity仍是可见的(即如果上面压着的是一个非全屏的Activity或透明的Activity),它是处于paused状态的。在系统内存不足的情况下,paused状态的Activity是有可被系统杀掉的。</span><br style="line-height:25px"><span style="line-height:25px">3.</span><span style="color:#000080; line-height:25px">几个事件的配对可以比较清楚地理解它们的关系。</span><span style="color:#993300; line-height:25px">Create</span><span style="color:#000080; line-height:25px">与</span><span style="color:#993300; line-height:25px">Destroy</span><span style="color:#000080; line-height:25px">配成一对,叫entrielifetime,在创建时分配资源,则在销毁时释放资源;往上一点还有</span><span style="color:#993300; line-height:25px">Start</span><span style="color:#000080; line-height:25px">与</span><span style="color:#993300; line-height:25px">Stop</span><span style="color:#000080; line-height:25px">一对,叫visiblelifetime,表达的是可见与非可见这么一个过程;最顶上的就是</span><span style="color:#993300; line-height:25px">Resume</span><span style="color:#000080; line-height:25px">和</span><span style="color:#993300; line-height:25px">Pause</span><span style="color:#000080; line-height:25px">这一对了,叫foregroundlifetime,表达的了是否处于激活状态的过程。</span><br style="line-height:25px"><span style="line-height:25px">4.</span>我们实现的Activity派生类的时候往往要重载两个重要的方法:<span style="color:#000080; line-height:25px">onCreate()进行初始化操作,onPause()保存当前操作的结果。</span><br style="line-height:25px"><br style="line-height:25px"> 除了ActivityLifecycle以外,Android还有一个ProcessLifecycle的说明:<br style="line-height:25px"><span style="color:#003366; line-height:25px">在内存不足的时候,Android是会主动清理门户的,那它又是如何判断哪个process是可以清掉的呢?文档中也提到了它的重要性排序:<br style="line-height:25px"> ClassOverview(android.app.Activity的文档中的类描述)</span><br style="line-height:25px"><span style="line-height:25px">1</span>.<span style="color:#000080; line-height:25px">最容易被清掉的是</span><span style="color:#993300; line-height:25px">emptyprocess</span><span style="color:#000080; line-height:25px">,空进程是指那些没有Activity与之绑定,也没有任何应用程序组件(如Services或者IntentReceiver)与之绑定的进程,也就是说在这个process中没有任何activity或者service之类的东西,它们仅仅是作为一个cache,在启动新的Activity时可以提高速度。它们是会被优先清掉的。因此建议,我们的后台操作,最好是作成Service的形式,也就是说应该在Activity中启动一个Service去执行这些操作</span>。<br style="line-height:25px"><span style="line-height:25px">2.</span><span style="color:#000080; line-height:25px">接下来就是</span><span style="color:#993300; line-height:25px">backgroundactivity</span><span style="color:#000080; line-height:25px">了,也就是被stop掉了那些activity所处的process,那些不可见的Activity被清掉的确是安全的,系统维持着一个LRU列表,多个处于background的activity都在这里面,系统可以根据LRU列表判断哪些activity是可以被清掉的,以及其中哪一个应该是最先被清掉。不过,文档中提到在这个已被清掉的Activity又被重新创建的时候,它的onCreate会被调用,参数就是onSaveInstanceState时的那个Bundle。【Activity被killed时,Android会帮它保留着这个Bundle。】</span><br style="line-height:25px"><span style="line-height:25px">3.</span><span style="color:#000080; line-height:25px">然后就轮到</span><span style="color:#993300; line-height:25px">serviceprocess</span><span style="color:#000080; line-height:25px">了,这是一个与Service绑定的进程,由startService方法启动。虽然它们不为用户所见,但一般是在处理一些长时间的操作(例如MP3的播放),系统会保护它,除非真的没有内存可用了。</span><br style="line-height:25px"><span style="line-height:25px">4.</span><span style="color:#000080; line-height:25px">接着又轮到那些</span><span style="color:#993300; line-height:25px">可见但不是在前台的activity</span><span style="color:#000080; line-height:25px">了,或者说visibleprocess。前面也谈到这个情况,被Paused的Activity也是有可能会被系统清掉,不过相对来说,它已经是处于一个比较安全的位置了。</span><br style="line-height:25px"><span style="line-height:25px">5.</span><span style="color:#000080; line-height:25px">最安全应该就是那个</span><span style="color:#993300; line-height:25px">foregroundactivity</span><span style="color:#000080; line-height:25px">了,不到迫不得已它是不会被清掉的。这种process不仅包括resume之后的activity,也包括那些onReceiveIntent之后的IntentReceiver实例。</span><br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意:所谓killactivity或killService是指kill它们所在的进程。<br style="line-height:25px"></wbr></span><wbr style="line-height:25px">在AndroidApplication的生命周期的讨论中,文档也提到了一些需要注意的事项:因为Android应用程序的生存期并不是由应用本身直接控制的,而是由Android系统平台进行管理的,所以,对于我们开发者而言,需要了解不同的组件Activity、Service和IntentReceiver的生命,切记的是:如果组件的选择不当,很有可能系统会杀掉一个正在进行重要工作的进程。</wbr></wbr></wbr></wbr></wbr></wbr>