Android Activity生命周期与启动模式

Activity生命周期


Activity生命周期图例

关于销毁的几个常见方法

  • activity.finish( )
    Call this when your activity is done and should be closed. The ActivityResult is propagated back to whoever launched you via onActivityResult().
    当你调用此方法的时候,系统只是将最上面的Activity移出了栈,并没有及时的调用onDestory()方法,其占用的资源也没有被及时释放。因为移出了栈,所以当你点击手机上面的“back”按键的时候,也不会再找到这个Activity。

  • onDestroy( )
    Perform any final cleanup before an activity is destroyed. This can happen either because the activity is finishing (someone called 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 isFinishing method.
    **Note: do not count on this method being called as a place for saving data!**For example, if an activity is editing data in a content provider, those edits should be committed in either onPause or onSaveInstanceState, not here. This method is usually implemented to free resources like threads that are associated with an activity, so that a destroyed activity does not leave such things around while the rest of its application is still running. There are situations where the system will simply kill the activity’s hosting process without calling this method (or any others) in it, so it should not be used to do things that are intended to remain around after the process goes away.
    系统销毁了这个Activity的实例,释放资源,节省内存空间!
    在Activity的生命周期中,onDestory()方法是他生命的最后一步,资源空间等就被回收了。当重新进入此Activity的时候,必须重新创建,执行onCreate()方法。
    注意:
    如API中所述,不要在onDestroy方法中进行计数等操作,因为调用finish()方法之后不一定会立即调用onDestroy()方法!
    finish()方法用于结束一个Activity的生命周期,而onDestory()方法则是Activity的一个生命周期方法,其作用是在一个Activity对象被销毁之前,Android系统统会调用该方法,用于释放此Activity之前所占用的资源。
    如果在调用finish()方法之后,针对Activity的状态进行一个计数操作(如常见在应用中的自定义Application中建立一个ArrayList对象ActivityList,用于管理应用中的Activity时),在onCreate()方法中add,在onPause()方法中利用Activity的isFinishing()方法进行判断,返回true时,才让ActivityList执行remove操作!
    Activity.isFinishing()
    Check to see whether this activity is in the process of finishing, either because you called finish on it or someone else has requested that it finished. This is often used in onPause to determine whether the activity is simply pausing or completely finishing.

  • System.exit(code)
    Causes the VM to stop running and the program to exit with the given exit status.
    退出整个应用程序,针对Application,直接Kill应用进程(有关Android进程和线程的知识,请参考:
    http://www.cnblogs.com/hanyonglu/archive/2012/04/12/2443262.html)。

开发者选项中“不保留活动”的开启对Activity的影响

不保留活动
在设备的开发者选项中,对应用程序的设置有一个“不保留活动”的设置,默认状态为:不勾选,解释如下:
Don’t keep activities( Destroy every activity as soon as the user leaves it)。
即,当勾选上时,设备中所有应用的Activity在用户离开当前Activity时,当前Activity执行onDestroy操作,立即摧毁掉!

Activity启动模式


四大模式简介

Activity有四种启动模式,在AndroidManifest.xml文件中的activity标签下设置android:launchMode属性即可。

  1. standard
    标准模式(缺省值)。在这个模式下,默认创建一个新的Activity实例,所以,允许多个相同的实例共存和叠加。
    原理图如下:
    standard

  2. singleTop
    如果目标Activity位于栈顶,不会创建新的实例,而是调用它的onNewIntent()方法启动之前的Activity,回到栈顶。所以,在这种模式下, 允许多个相同的实例共存,但是不允许叠加。
    目标Activity位于栈顶的情况如下:

    目标Activity没有位于栈顶的情况如下:
    这里写图片描述

  3. singleTask
    如果当前栈中存在目标Activity,则不会创建新的实例,而是将目标Activity所在位置之上的其他Activity移除,使目标Activity成为栈顶。在这种模式下,不允许存在多个相同的实例。
    原理图如下:

  4. singleInstance
    目标Activity在整个系统中只有一个实例,并且独自霸占一个栈。所以,在这种模式下,目标Activity被启动时,会首先判断系统其他栈中是否有目标Activity,如果有并且该栈中只有目标Activity一个实例,则启动这个实例。
    原理图如下:
    这里写图片描述
    注意操作方式和返回结果:
    这里写图片描述
    singleTask与singleInstance的区别在于:前者强调的是系统范围内的实例唯一,后者局限于当前Activity栈中的实例唯一。

应用场景联想

singleTop适合从接受的通知启动的界面。比如,新闻客户端的推送通知,使用singleTop能够避免点击多条导致多次启动相同页面的情况。

singleTask适合应用的主界面。例如浏览器的主界面。不管从多少个应用启动浏览器,只会启动主界面一次,其余情况都会走onNewIntent,并且会清空主界面上面的其他页面。

singleInstance适合与程序分离开的界面。例如闹铃提醒,将闹铃提醒与闹铃设置分离。singleInstance不要用于中间页面,如果用于中间页面,跳转会有问题,比如:A -> B (singleInstance) -> C,完全退出后,在此启动,首先打开的是B。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值