![](/img/trans.png)
[英]will onDestroy() of activity always be called on application destroy?
[英]Android Activity onDestroy() is not always called and if called only part of the code is executed
onDestroy()
并不总是被调用。 如果被调用,则仅执行部分代码。 而且在LogCat中 ,大多数时候我只会看到消息“ gps销毁状态首先被调用”。 这是为什么?
protected void onDestroy() {
super.onDestroy();
Log.d("on destroy called", "gps state on destroy called first");
editor.putBoolean("gpsOn", false);
Log.d("on destroy called", "gps state on destroy called second");
editor.commit();
Log.d("on destroy called", "gps state on destroy called third");
stopRouteTracking();
Log.d("on destroy called", "gps state on destroy called fourth");
}
看看这个:
和这个:
http://developer.android.com/reference/android/app/Activity.html#onDestroy%28%29
基本上,永远无法保证onDestroy()
将被调用,并且在某些情况下,诸如应用程序之类的进程将直接被杀死,无论如何都要绕过方法调用。
在此处的android开发人员文档中,您可以看到-
对于那些标记为可杀死的方法,该方法返回后,承载活动的进程可能会在不执行其代码另一行的情况下,随时被系统杀死。 因此,您应该使用onPause()方法将任何持久性数据(例如用户编辑)写入存储。
onStop()和onDestroy()都标记为可杀死。
这可能是因为只调用部分用onDestroy()编写的代码的原因,因为进程在执行onStop()之后可以随时销毁。
super.onDestroy()
的答案是正确的,但是在调用代码之前,对super.onDestroy()
的调用可能会导致只调用部分代码的问题。 super.onDestroy()
应该在末尾被调用,因为这样您的代码将在销毁之前被调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.