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
不应运行可能需要很长时间的代码。 - Hugo Hideki Yamashitasuper.onDestroy()
移动到方法的最后一行。 - Pierre