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

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

问题描述

我读过一些文章,指出使用此方法不好",不应使用,这不是关闭"应用程序的正确方法,也不是Android的工作方式...

I've read some posts stating that using this method is "not good", shouldn't be used, 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 the Android OS knows better than me when it's the right time to terminate the process, but I haven't yet heard a good explanation on why it's wrong to use the killProcess() method. After all - it's part of the Android API.

我所知道的是,在其他线程正在执行潜在的重要工作(对文件的操作,写入数据库,HTTP请求,正在运行的服务..)时调用此方法会导致线程被终止,这显然是不好的.另外,我知道我可以从以下事实中受益:重新打开"应用程序将更快,因为从上次使用该应用程序以来,系统仍将保持"状态保持在内存状态,而killProcess()可以防止这种情况.

What I do know is that calling this method while other threads are doing potentially important work (operations on files, writing to DB, HTTP requests, running services..) results in the threads being terminated and it's clearly not good. Also, I know I can benefit from the fact that "re-opening" the application will be faster because the system stills "holds" in memory state from the last time the app was used, and killProcess() prevents that.

除了这个原因之外,假设我没有这样的操作,并且我不在乎我的应用程序是否每次打开应用程序都会从​​头开始,还有其他原因为什么我不应该使用killProcess()方法?

Besides this reason, assuming I don't have such operations, and I don't care my application whether my app will start from scratch every time it is opened, are there other reasons why I should not use the killProcess() method?

我知道关闭Activityfinish()方法,因此请不要在答案中包括该内容.

I know about the finish() method to close an Activity, so please don't include that in your answer.

finish()仅适用于Activity,不适用于所有应用程序,我想我确切地知道为什么以及何时使用它.

finish() is only for Activity, not for all applications, and I think I know exactly why and when to use it.

还有另一件事-我正在使用Unity3D框架开发游戏,并将该项目导出到Android.当我反编译生成的apk时,我很惊讶地发现从unity创建的Java源代码-用Process.killProcess(Process.myPid())实现Unity的-Application.quit()方法.

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

Application.quit()被认为是关闭游戏的正确方法(真的吗?也许我错了并且错过了一些东西),那么为什么Unity的框架开发人员会在本机Android中实现此功能?

Application.quit() is supposed to be the right way to close the game according to the Unity3D docs (Is it really? Maybe I'm wrong and missed something), so why did the Unity's framework developers implement this in native Android?

推荐答案

谁说调用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调用而不是解决方法时,Google并没有提到最好不要在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天全站免登陆