在Ondestroy()中驳回AlertDialog?

发布于 2025-02-09 10:51:05 字数 206 浏览 2 评论 0原文

是否有必要在您的活动中对您产卵的AlertDialog进行参考,并在OnDestroy()中将其删除,或者您可以创建它并忘记它吗?当您离开活动时,它似乎被驳回了吗?

我想AlertDialog通常对活动上下文的参考,这可能会阻止活动收集垃圾。在这里避免可能的内存泄漏的过程是什么?

AlertDialog是否属于活动,即它遵循活动生命周期,在活动的同时被摧毁等...?

Is it necessary to keep a reference to your spawned AlertDialog in your Activity and dismiss it in onDestroy() or could you just create it and forget it? It seems to be dismissed when your leave the Activity?

I suppose the AlertDialog holds a reference to the Activity Context normally, which might prevent the Activity from being garbage collected. What is the procedure to avoid possible memory leak here?

Does an AlertDialog belong to the Activity, i.e. does it follow the Activity lifecycle, being destroyed at the same time as the Activity and so on...?

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

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

发布评论

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

评论(1

_蜘蛛 2025-02-16 10:51:06

我认为,如果您使用活动上下文或活动范围催生“警报”对话框...每当活动被销毁时,它将自动销毁。
当您想从无法访问活动上下文的可重复使用的实用程序类中产生警报对话框并最终使用全局应用程序上下文时,您可能需要手动销毁对话框,因为它可能会超过当前的当前,因此可能会出现问题活动,导致泄漏。

I think, if you spawned the alert Dialog using the activity context or activity scope... It will be automatically destroyed whenever the activity is destroyed.
Maybe the problem arises when you want to spawn an Alert dialog from a reusable Utility class which doesn't have access to activity context, and ended up using Global Application context, then you need to manually destroy the dialog, since it may outlive the current activity, causing leak.

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