本文介绍了使用system.exit(0)的onDestroy()(安卓)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,这是不推荐在一个Android应用程序中使用system.exit(0),但我想知道,如果这将是确定使用它的onDestroy()因为应用程序允许在这一点上被杀害?

I know that it is not recomended to use system.exit(0) in an Android application, but I was wondering if it would be ok to use it in onDestroy() since the application is allowed to be killed at that point?

我问的原因是关于this题。

The reason I am asking is in relation to this question.

推荐答案

  • 请不要依赖的的onDestroy 将被调用。在有些情况下,操作系统将杀了你的应用程序,而无需调用你的的onDestroy

  • Do not rely that onDestroy will be called. There are situations where the OS will kill your application without calling your onDestroy.
  • 建议只是一个字的,当然,因为它并不直接适用于您的方案。如果 system.exit(0)不叫,因为该应用程序已被杀害,我说你真行。 ; - )

    Just a word of advice, of course, as it does not directly apply to your scenario. If system.exit(0) was not called because the app is already killed, I'd say you're OK. ;-)

    • 如果你的 Activity.onDestroy 被称为,这意味着你的过程是两种状态之一:
    • If your Activity.onDestroy is called, that means your process is in one of two states:

    - 有没有其他成分(服务的ContentProvider )的应用程序运行的操作系统是知道的。这意味着你的过程是最有可能会被OS杀死立即反正,或将成为第一个被收回,如果系统的其他部件/其他应用程序需要的物理内存。因此调用退出(0)将实现并不多。

    -- there's no other component (Service or a ContentProvider) running in the app that the OS is aware of. This means that your process is most likely going to be killed by the OS immediately anyway, or will be the first one to be reclaimed if other parts of the system/other apps need physical memory. Thus calling exit(0) will achieve not much.

    - 有一个在你的过程中,该系统是知道运行的另一个组成部分。呼叫退出()在这种情况下,将终止该进程,杀死你的其他组件,并可能破坏你的数据。操作系统可以不在乎,当然,但你的用户可能无法AP preciate它。 : - )

    -- there's another component running in your process that the system is aware of. Calling exit() in this case would terminate the process, killing your other component and potentially corrupting your data. The OS could care less of course, but your users might not appreciate it. :-)

    • 杀死自愿的过程中会不会帮助其他应用程序,如果他们已经耗尽了他们内部的Dalvik的堆限制。无论设备多少物理内存了,操作系统有多少内存的Dalvik是允许在任何过程中使用的堆分配的限制。因此,有可能的是,系统有其存储器的自由半部和特定的应用仍然命中OOM。

    在Dalvik的堆限制是OEM可配置的,但很少有原始设备制造商实际上做作出努力,以调整其设置为他们的设备,只是去与操作系统的默认值。我不记得具体的默认值,但它是安全的假设每个应用程序被允许16MB的低端升级Froyo /姜饼和48MB高端ICS / JB手机之间。哎呀,我很乐观,碰到上限为128MB(虽然我还没有听到这样的设备)。 : - )

    The Dalvik heap limit is OEM configurable, though very few OEMs actually do make the effort to tune it up for their devices, and just go with the OS defaults. I don't remember the specific defaults, but it's safe to assume each app is allowed between 16MB on low-end Froyo/Gingerbread and 48MB on high-end ICS/JB phones. Heck, I'd be optimistic and bump the upper limit to 128MB (though I am yet to hear of a such a device) . :-)

    • 如果您的应用程序有问题的内存使用情况(所链接的问题隐含的),你试图通过强制上的活动的下一个开放一个干净的石板去解决它,这种方法是可行的。有点。但它是怎么回事,只要你的应用程序只使用活动工作。您在的ContentProvider 服务,你不能叫退出抛出的瞬间()了(正如我上面提到的),你将被迫妥善解决问题。你还不如咬咬牙,现在就行动。最简单的办法是,以确保您呼叫 Bitmap.recycle() 当您使用特定的位图就大功告成了。当然,这工作,只要你没有让更多的位图的内存比你的内存,但是这是一个完全独立的兽共反正。 : - )
    • If your application has problems with its memory usage (implied by the linked question) and you are trying to solve it by forcing a "clean" slate on the next opening of the activity, this approach would work. Kind of. But it's going to work for as long as your application is using activities only. The moment you throw in a ContentProvider or a Service, you can't afford calling exit() anymore (as I mentioned above), and you will be forced to solve the problem properly. You might as well just bite the bullet and do it now. The simplest approach would be to ensure you are calling Bitmap.recycle() when you're done with the particular bitmap. Of course, that works as long as you don't have to keep more bitmaps in memory than you have memory, but that's a totally separate beast altogether anyway. :-)

    这篇关于使用system.exit(0)的onDestroy()(安卓)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-28 11:30
查看更多