活动OnDestroy从来没有打电话?

我在我的ListActivity中使用以下代码

// a separate class in project public class MyActivity extends ListActivity { // some common functions here.. } public class SelectLocation extends MyListActivity { public void onCreate(Bundle savedInstance) { // here..... } @Override protected void onDestroy() { super.onDestroy(); if (adap != null) adap = null; if (list != null) list = null; System.gc(); } } 

任何一个指导我为什么onDestroy方法不在我的代码中调用?

onDestroy()仅在系统资源不足(内存,CPU时间等)时被调用,并决定终止活动/应用程序,或者当有人调用finish()来完成活动时。

所以,为了testing你的代码(),你可以做一个testingbutton,这将调用你的活动finish()

在这里阅读更多。

另外,我相信你不需要在onDestroy()调用所有这些东西,直到适配不是关键资源。 即使在这种情况下,Android系统有正确处置它们的机制。

不能保证你的onDestroy方法将被调用。

您在onDestroy方法中使用的代码根本不需要。 如果destroy被调用,你的活动将被从堆栈中移除,并且无论如何都可以被垃圾回收所有的资源所吸引,只有活动被引用。 另外System.gc()应该是不好的风格。 在Android上,系统几乎总是知道什么时候进行垃圾回收。 活动完成垃圾回收的大部分时间都是自动触发的。 只要删除整个onDestroy方法。 如果你的应用程序的整体内存有问题,那么问题就在其他地方。

在大多数手机中,当按下后退button时,会调用两次onStop()和onDestroy()方法,但如果不是这种情况,可以创build一个button来调用finish(); 方法。