【发布时间】:2013-08-21 15:34:23
【问题描述】:
onDestroy() 并不总是被调用。如果调用,则只执行部分代码。在LogCat 中,大多数时候我只看到消息“gps state on destroy called first”。这是为什么呢?
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");
}
【问题讨论】:
-
您是否尝试过将此代码放在
onStop而不是onDestroy上?onStop在onDestroy之前被调用。而且,onDestroy不应该运行可能需要很长时间的代码。 -
developer.android.com/training/basics/activity-lifecycle/…查看官方文档中的Activity生命周期
-
@HugoHidekiYamashita,我还没有尝试将它放在 onStop 上。我会尝试并删除 stopRouteTracking() 以查看它是如何工作的。
-
@Pavlos,来自官方文档,有:注意:不要指望这个方法被称为保存数据的地方!所以我认为 onDestroy() 并不是每次都被调用。
-
这里大家都这么说!