gpt4 book ai didi

android - 如果 Asynctask 正在运行,即使在 finish() 方法之后 Activity 是否仍然存在?

转载 作者:太空宇宙 更新时间:2023-11-03 11:42:38 25 4
gpt4 key购买 nike

On the long run, AsyncTask produces a memory leak : if the AsyncTask lasts for long, it keeps the activity "alive" whereas Android would like to get rid of it as it can no longer be displayed. The activity can't be garbage collected and that's a central mechanism for Android to preserve resources on the device.

这是否意味着如果我们调用 finish() 方法并且 asynctask 正在运行,那么 Activity 也将保持“Activity ”状态?如果不是,那么这意味着什么

if the AsyncTask lasts for long, it keeps the activity "alive"?

除非我们调用 finish() 方法或按下后退按钮,否则 Activity 将处于 Activity 状态。这是否意味着如果 asyntask 正在运行,那么 Activity 甚至在此之后仍将处于 Activity 状态?

最佳答案

如果 AsyncTask 被声明为 Activity 内部的非静态内部类,这意味着它始终持有对创建它的封闭外部类的强引用。因此,只要 AsyncTask 正在运行,它就会持有对 Activity 的强引用,这意味着即使在调用 finish() 并执行 onDestroy() 之后,它也不会被垃圾回收。这是 AsyncTask 的一个非常常见的问题,没有正确实现以防止这种情况发生。

关于android - 如果 Asynctask 正在运行,即使在 finish() 方法之后 Activity 是否仍然存在?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36028340/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com