Activity.finish()方法到底在做什么? [英] What is Activity.finish() method doing exactly?

查看:95
本文介绍了Activity.finish()方法到底在做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发Android应用程序一段时间,并关注了很多有关活动生命周期以及应用程序生命周期的文章.

I'm developing android applications for a while, and followed a lot of posts about activity life cycle, and application's life cycle.

我知道Activity.finish()方法会调用Activity.onDestroy()的某个位置,并且还会从堆栈中删除该活动,并且我猜想它以某种方式指向了操作系统和垃圾收集器,他可以尽其所能"并释放内存,当它找到它的好时机....

I know Activity.finish() method calls somewhere in the way to Activity.onDestroy(), and also removing the activity from stack, and I guess it somehow points to operating system and garbage collector that he can "do his trick" and free the memory when it find it a good time doing so....

我来到了这篇文章- 正在退出的应用程序不满意吗?并阅读Mark Murphy的答案

I came to this post - Is quitting an application frowned upon? and read Mark Murphy's answer.

这让我对finish()方法的实际作用有些困惑.

It made me a bit confused about what exactly the finish() method actually does.

有没有可能我会呼叫finish()而不会呼叫onDestroy()?

Is there a chance I'll call finish() and onDestroy() won't be called?

推荐答案

在活动上调用 finish() 时,将执行方法 onDestroy() .此方法可以执行以下操作:

When calling finish() on an activity, the method onDestroy() is executed. This method can do things like:

  1. 关闭该活动正在管理的所有对话框.
  2. 关闭活动正在管理的所有游标.
  3. 关闭所有打开的搜索对话框

此外, onDestroy() 不是析构函数.它实际上并没有破坏对象.这只是基于特定状态调用的方法.因此,在超类的onDestroy()运行并返回后,您的实例仍然有效并且运行良好.Android会保留进程,以防用户想要重新启动应用程序,这将加快启动阶段.该进程将不做任何事情,如果需要回收内存,该进程将被杀死

Also, onDestroy() isn't a destructor. It doesn't actually destroy the object. It's just a method that's called based on a certain state. So your instance is still alive and very well* after the superclass's onDestroy() runs and returns.Android keeps processes around in case the user wants to restart the app, this makes the startup phase faster. The process will not be doing anything and if memory needs to be reclaimed, the process will be killed

这篇关于Activity.finish()方法到底在做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆