为什么叫Process.killProcess(Process.myPid())是一个坏主意? [英] Why calling Process.killProcess(Process.myPid()) is a bad idea?

查看:216
本文介绍了为什么叫Process.killProcess(Process.myPid())是一个坏主意?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读过一些帖子说,使用这种方法是不好,不应该被使用,这是不正确的方式关闭的应用程序,它不是如何Android的作品...

I've read some posts saying using this method is "not good", shouldn't been use, it's not the right way to "close" the application and it's not how android works...

我理解并接受Android操作系统更知道我,然后当它是正确的时间,以终止的进程的事实,但我没有听说又一个很好的解释为什么使用killProcess()方法是错的?毕竟 - 这是Android的API的一部分......

I understand and accept the fact that Android OS knows better then me when it's the right time to terminate the process, but I didn't heard yet a good explanation why it's wrong using the killProcess() method?. After all - it's part of the android API...

我所知道的是,在调用此方法,而其他线程在做潜在的一项重要工作(业务上的文件,写入数据库,HTTP请求,正在运行的服务。)可以在中间被终止,这显然不是好。我也知道,我可以从一个事实,即重新打开应用程序会更快受益,导致系统也许仍然是持有从上次记忆状态被使用,killProcess()prevents的。

what I do know is that calling this method while other threads doing in potential an important work (operations on files, writing to DB, HTTP requests, running services..) can be terminated in the middle, and it's clearly not good. also I know I can benefit from the fact that "re-open" the application will be faster, cause the system maybe still "holds" in memory state from last time been used, and killProcess() prevents that.

除了这个原因,假设我没有这样的操作,我不在乎我的应用程序将从头开始每次运行时加载,还有为什么不使用其他原因 killProcess()的方法?

Besides this reason, assuming I don't have such operations, and I don't care my application will load from scratch each run, there are other reasons why not using the killProcess() method?

我知道完成()方法来关闭一个活动,所以不要写我,那请.. 完成()仅适用于活动。不是所有的应用程序,我想我知道为什么以及何时使用它......

I know about finish() method to close an Activity, so don't write me about that please.. finish() is only for Activity. not to all application, and I think I know exactly why and when to use it...

和另一件事 - 我也开发游戏与Unity3D框架,出口项目到Android。当我反编译生成APK,我很惊讶地发现,Java源代码code从统一创建 - 实施统一的 - Application.quit()法,用 Process.killProcess(Process.myPid())

And another thing - I'm developing also games with the Unity3D framework, and exporting the project to android. When I decompiled the generated apk, I was very suprised to find out that the java source code created from unity - implementing Unity's - Application.quit() method, with Process.killProcess(Process.myPid()).

Application.quit()被假设是正确的方式,根据Unity3d导游收的游戏(真的是这样吗?也许我错了,错过了一些),那么它是如何发生的了统一的框架,开发其做了很好的工作,因为它似乎实现了这个在原生的Andr​​oid为 killProcess()

Application.quit() is suppose to be the right way to close game according to Unity3d guides (is it really? maybe I am wrong, and missed something), so how it happens that the Unity's framework developers which doing a very good work as it seems implemented this in native android to killProcess()?

推荐答案

谁说调用Process.killProcess(Process.myPid())是一个坏主意?

Who said calling Process.killProcess(Process.myPid()) is a bad idea?

是的,让操作系统管理它自己的内存是为您和您的使用应用程序的用户的最佳实践(更快的再次打开,为力闭合机会少,等...)。

Yes, letting the OS manage its own memory is the best practice for both you and the user using your application (faster to open again, less chances for force closes, etc...).

不过,假设你肯定知道你不中断线程或其他后台操作,并使用此调用的onDestroy() - 我看不出有什么理由你不应该使用它。特别是当它的API调用,而不是一个解决办法,而谷歌并没有提到它的最好不要使用它的API文档。

However, assuming you know for sure that you're not interrupting threads or other background operations and you use this call in onDestroy() - I see no reason why you shouldn't use it. Especially when it's an API call and not a workaround, and Google didn't mention it's better not to use it in the API documentation.

这篇关于为什么叫Process.killProcess(Process.myPid())是一个坏主意?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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