【Android】关于Activity的生命周期及一些理解

</pre></div><div style="background-color:inherit"><span class="token p" style="color:rgb(90,90,90); font-family:'microsoft yahei'; font-size:18px; line-height:29.7px; white-space:pre-wrap; outline:none!important"></span><div style="background-color: inherit;"><span style="font-family: 微软雅黑; font-size: 14px; line-height: 21px; background-color: inherit;"><span class="token p" style="box-sizing: border-box; color: rgb(90, 90, 90); font-family: 'microsoft yahei'; font-size: 18px; line-height: 29.7px; white-space: pre-wrap; outline: none !important;">一直对写博客不怎么感冒,但希望这是一个好的开始!</span></span></div><div style="background-color: inherit;"><span style="font-family: 微软雅黑; font-size: 14px; line-height: 21px; background-color: inherit;"><span class="token lf" style="box-sizing: border-box; color: rgb(90, 90, 90); font-family: 'microsoft yahei'; font-size: 18px; line-height: 29.7px; white-space: pre-wrap; outline: none !important;"></span><span class="token p" style="box-sizing: border-box; color: rgb(90, 90, 90); font-family: 'microsoft yahei'; font-size: 18px; line-height: 29.7px; white-space: pre-wrap; outline: none !important;">记录一下自己对Android知识的理解,欢迎大家拍砖,评论交流,一起进步!!</span></span></div><div style="background-color: inherit;"></div><div style="background-color: inherit;"><span style="font-family: 微软雅黑; font-size: 14px; line-height: 21px; background-color: inherit;"></span></div><div style="background-color: inherit;"><span style="font-family: 微软雅黑; font-size: 14px; line-height: 21px; background-color: inherit;">一、Activity的生命周期</span></div></div><div><div style="font-family:微软雅黑; font-size:14px; line-height:21px; background-color:inherit">1)onCreate:表示Activity正在被创建,生命周期的第一个方法。</div><div style="font-family:微软雅黑; font-size:14px; line-height:21px; background-color:inherit">2)onRestart:表示Activity正在重新启动。当前Activity从不可见重新变为可见状态时,onRestart就会被调用。</div><div style="font-family:微软雅黑; font-size:14px; line-height:21px; background-color:inherit">3)onStart:表示Activity正在被启动,即将开始,这时<span style="color: rgb(255, 0, 0); background-color: inherit;">Activity已经可见</span>了,但是<span style="color:#ff0000; background-color:inherit">还没有出现在前台</span>,还无法和用户交互。</div><div style="font-family:微软雅黑; font-size:14px; line-height:21px; background-color:inherit">4)onResume:表示<span style="color:#ff0000; background-color:inherit">Activity已经可见</span>了,并且出现<span style="color:#ff0000; background-color:inherit">在前台并开始活动</span>。</div><div style="font-family:微软雅黑; font-size:14px; line-height:21px; background-color:inherit">5)onPause:表示Activity正在停止,紧接着onStop就会被调用。可以做一些存储数据、停止动画等工作,但不能太耗时。onPause必须先执行完,新Activity的onResume才会执行。</div><div style="font-family:微软雅黑; font-size:14px; line-height:21px; background-color:inherit">6)onStop:表示Activity即将停止,可以做一些稍微重量级的回收工作,同样不能太耗时。</div><div style="font-family:微软雅黑; font-size:14px; line-height:21px; background-color:inherit">7)onDestroy:表示Activity即将被销毁,是Activity生命周期中的最后一个回调,可以做一些回收工作和最终的资源释放。</div><div style="font-family:微软雅黑; font-size:14px; line-height:21px; background-color:inherit"><img src="https://img-blog.csdn.net/20160624171012644?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" /><img src="file:///C:/Users/suning.tang/AppData/Local/YNote/data/tangsulong24@126.com/ced814865acd4b358fd55127b7bbef17/clipboard.png" alt="" style="display:inline-block; margin-top:8px; max-width:800px; line-height:1.5; background-color:inherit" /></div><div style="font-family:微软雅黑; font-size:14px; line-height:21px; background-color:inherit"></div><div style="font-family:微软雅黑; font-size:14px; line-height:21px; background-color:inherit">注:透明的Activity不会回调onStop</div><div style="font-family:微软雅黑; font-size:14px; line-height:21px; background-color:inherit"><br style="background-color:inherit" /></div><h1 style="font-family:微软雅黑; font-size:14px; line-height:21px; background-color:inherit">二、异常情况下的生命周期分析</h1><div style="font-family:微软雅黑; font-size:14px; line-height:21px; background-color:inherit"><img src="https://img-blog.csdn.net/20160624171030725?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" /></div><div style="font-family:微软雅黑; font-size:14px; line-height:21px; background-color:inherit"><img src="file:///C:/Users/suning.tang/AppData/Local/YNote/data/tangsulong24@126.com/42fffdff439046f4a43526b2d75c05b2/clipboard.png" alt="" style="display:inline-block; margin-top:8px; max-width:800px; background-color:inherit" /></div><div style="font-family:微软雅黑; font-size:14px; line-height:21px; background-color:inherit"><span style="background-color:inherit">1) 当系统配置发生改变之后,</span><span style="color:#ff0000; background-color:inherit">Activity会被销毁,其onPause,(onSaveInstanceState),onStop,onDestroy均会被调用</span>,同时由于<span style="background-color:inherit">Activity是在异常情况下终止</span>的,系统会<span style="background-color:inherit">在onStop之前调用onSaveInstanceState</span>来保存当前的Activity的状态。</div><div style="font-family:微软雅黑; font-size:14px; line-height:21px; background-color:inherit">2) 当Activity被重新创建后,系统会调用onRestoreInstanceState,并且把Activity销毁时onSaveInstanceState方法保存的Bundle对象作为参数同时传递给onRestoreInstanceState和onCreate方法。</div><div style="font-family:微软雅黑; font-size:14px; line-height:21px; background-color:inherit"><span style="background-color:inherit">从时序上来说,</span><span style="color:#ff0000; background-color:inherit">onRestoreInstanceState的调用时机在onStart之后</span>。</div><div style="font-family:微软雅黑; font-size:14px; line-height:21px; background-color:inherit">3) 当系统内存不足时,系统也会按照上述优先级去杀死目标Activity所在的进程,并后续通过onSaveInstanceState和onRestoreInstanceState来存储和恢复数据。</div><div style="font-family:微软雅黑; font-size:14px; line-height:21px; background-color:inherit">(上面两张图和主要内容来自于著名的《<span style="font-family:微软雅黑; font-size:14px; line-height:21px">Android开发艺术探索</span>》哈~~)</div><h1 style="font-family:微软雅黑; font-size:14px; line-height:21px; background-color:inherit">三、常见的应用问题分析</h1><h2 style="font-family:微软雅黑; font-size:14px; line-height:21px; background-color:inherit">    1.如果一个Activity在用户可见时才处理某个广播,不可见时注销掉,那么应该在哪个生命周期的回调方法去注册和注销BroadcastReeiver呢?</h2><div style="font-family:微软雅黑; font-size:14px; line-height:21px; background-color:inherit">        Activity的可见生命周期发生在onStart调用和onStop调用之间。在这段时间,用户可以在屏幕上看到Activity并与其交互。因此,我们可以在onStart中注册一个BroadcastReceiver以监控影响UI的变化,并在用户无法看到您显示的内容时在onstop中将其取消注册。</div><div style="font-family:微软雅黑; font-size:14px; line-height:21px; background-color:inherit">        为什么不能放在onPause方法中?</div><div style="font-family:微软雅黑; font-size:14px; line-height:21px; background-color:inherit">        原因是onPause较容易被触发,如弹出一个Dialog对话框时,Activity界面仍然是可见的,但onPause方法会被调用。所以,在保存数据时,为保证数据存储的有效性可以放在onPause去做。在某些情况下,Activity走完onPause后,还未执行到onStop就被系统回收了。</div><div style="font-family:微软雅黑; font-size:14px; line-height:21px; background-color:inherit">        注意:<span style="color:#ff0000; background-color:inherit">onPause中要非常迅速地执行完所需操作,否则会影响下一个Activity的生命周期函数的调用</span>。</div><div style="font-family:微软雅黑; font-size:14px; line-height:21px; background-color:inherit">        从Activity A启动Activity B时,它们的生命周期发生顺序是:</div><div style="font-family:微软雅黑; font-size:14px; line-height:21px; background-color:inherit"><span style="background-color:inherit">        </span><span style="color:#ff0000; background-color:inherit">A.onPause()</span> -> <span style="color:#0070c0; background-color:inherit">B.onCreate() </span>-> <span style="color:#0070c0; background-color:inherit">B.onStart()</span> -> <span style="color:#0070c0; background-color:inherit">B.onResume()</span> -> B显示,同时A在屏幕上不可见 -> <span style="color:#ff0000; background-color:inherit">A.onStop()</span>.</div><div style="font-family:微软雅黑; font-size:14px; line-height:21px; background-color:inherit"><span style="background-color:inherit">        由上面的流程可知,</span><span style="color:#ff0000; background-color:inherit">启动第B的过程与停止A的过程存在重叠</span>。</div><div style="font-family:微软雅黑; font-size:14px; line-height:21px; background-color:inherit"></div><h2 style="font-family:微软雅黑; font-size:14px; line-height:21px; background-color:inherit"><span style="background-color:inherit">    2.finish和onDestory方法区别?</span></h2><div style="font-family:微软雅黑; font-size:14px; line-height:21px; background-color:inherit">        先看看Activity的源码注释是怎么说的<span style="line-height:1.5">    </span></div><div><pre><pre code_snippet_id="1728728" snippet_file_name="blog_20160624_2_6375185" name="code" class="java">/**
 * Call this when your activity is done and should be closed.
 */
public void finish() {...}
 
 
    在你的activity动作完成的时候,或者是activity需要关闭的时候调用此方法。
    也就是说,调用些方法之后,系统只是将该Activity从栈中移除了,但并没有及时的调用onDestory方法,其还占用着内存资源。此时点击手机上的back键,再也找不到这个Activity了,因为它已经移出栈。

/**
 * Perform any final cleanup before an activity is destroyed.  This can
 * happen either because the activity is finishing (someone called
 * {@link #finish} on it, or because the system is temporarily destroying
 * this instance of the activity to save space.  You can distinguish
 * between these two scenarios with the {@link #isFinishing} method.
 */
protected void onDestroy() {...}
    调用这个方法后,系统会销毁这个Activity的实例在内存中占据的空间。重新进入该Activity时,系统会执行onCreate方法进行重新创建。
    调试可以发现,执行finish会调用到onDestory方法,即 finish() -> onPause() -> onStop() -> onDestory()。
     finish()方法用于主动结束一个Activity的生命周期,而onDestory()方法则是Activity生命周期的中最后一步,调用该方法后,系统会释放此Activity之前占用的内存资源
 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值