Android Activity类中的finish()、onDestory()和System.exit(0) 三者的区别

Activity类中的finish()、onDestory()和System.exit(0) 三者的区别:

finish是Activity的类,仅仅针对Activity,当调用finish()时,只是将活动推向后台,并没有立即释放内存,活动的资源并没有被清理;当调用System.exit(0)时,杀死了整个进程,这时候活动所占的资源也会被释放。

 

Activity.finish()

Call this when your activity is done and should be closed. 

在你的activity动作完成的时候,或者Activity需要关闭的时候,调用此方法。

当你调用此方法的时候,系统只是将最上面的Activity移出了栈,并没有及时的调用onDestory()方法,其占用的资源也没有被及时释放。因为移出了栈,所以当你点击手机上面的“back”按键的时候,也不会找到这个Activity。

 

Activity.onDestory()

the system is temporarily destroying this instance of the activity to save space.

系统销毁了这个Activity的实例在内存中占据的空间。

在Activity的生命周期中,onDestory()方法是他生命的最后一步,资源空间什么的都没有咯~~。当重新进入此Activity的时候,必须重新创建,执行onCreate()方法。

 

System.exit(0)

这玩意是退出整个应用程序的,是针对整个Application的。将整个进程直接KO掉。

使用时,可以写在onDestory()方法内,亦可直接在想退出的地方直接调用:

如:System.exit(0); 或 android.os.Process.killProcess(android.os.Process.myPid());

 

文档介绍:

this.finish() in Activity only stop and destroy this activity, application still staying background (check by hold HOME button) 
I used: 
System.exit(0); 
but it still appear there! 

That's how Android works. The user/developer is not given any way to actually exit the application. When you call 'finish', the application stack is just pushed to the background. It still exists in the memory. Android itself decides when to close the application(i.e. remove its instance from the memory) and generally this is done when your application becomes the oldest application which was not used for the longest time.

 

贴一张Activity生命周期图:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值