Activity OnDestroy 从未调用过?

发布于 2024-10-08 05:08:13 字数 541 浏览 0 评论 0原文

我在 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 方法吗?

I am using following code in my 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();
    }
}

any one guide me why onDestroy method is not called in my code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

倾`听者〃 2024-10-15 05:08:13

仅当系统资源(内存、CPU 时间等)不足并决定终止您的 Activity/应用程序或有人调用 finish() 时,才会调用 onDestroy()代码> 您的活动。

因此,要测试您的代码,您可以创建一个测试按钮,它将在您的活动上调用 finish()

请在此处了解更多信息。

另外,我相信您不需要在 onDestroy() 中调用所有这些内容,直到 adap 不是关键资源。即使在这种情况下,Android 系统也有适当处理它们的机制。

onDestroy() is called only when the system is low on resources (memory, cpu time, etc) and makes a decision to kill your activity/application or when somebody calls finish() on your activity.

So, to test your code you can make a test button, that will call finish() on your activity.

Read more here.

Also, I believe you don't need to call all this stuff in onDestroy() until adap is not a critical resource. And even in that case the android system has mechanisms to properly dispose of them.

墨落成白 2024-10-15 05:08:13

无法保证您的 onDestroy 方法将被调用。

根本不需要您在 onDestroy 方法中使用的代码。如果调用 destroy,您的活动将从堆栈中删除,并且可以免费进行垃圾回收,其中的所有资源仅由活动引用。另外 System.gc() 应该是不好的风格。在 Android 上,系统几乎总是知道什么时候是进行垃圾收集的最佳时机。大多数时候,活动完成垃圾收集是自动触发的。只需删除整个 onDestroy 方法即可。如果您的应用程序的整体内存出现问题,则问题出在其他地方。

There is no guarantee that your onDestroy method will be called at all.

The code that you are using in your onDestroy method is not needed at all. If destroy is called your acitivity will be removed from the stack and is free for garbage collection anyway with all the resources in it that are only referenced by the activity. Also System.gc() is supposed to be bad style. On Android the system nearly always knows when it is the best time to do a garbage collection. Most of the times an activity finishes garbage collection is triggered automatically. Just remove the whole onDestroy method. If you have problems with the overall memory of your application the problem is somewhere else.

战皆罪 2024-10-15 05:08:13

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

In most phones when the back button is pressed there are called twice onStop() and onDestroy() methods, but if it isn't your case, you can create a button to invoke the finish(); method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文