阅读时the example ,我注意到作者在MyActivity
中创建了Runnable task
。如果我关闭该 Activity ,系统将无法对其进行垃圾收集,因为可运行对象包含对 MyActivity 的隐式引用,对吗?只要可运行程序有效,该 Activity 就会一直存在。
如有错误,请指正。
最佳答案
看起来是正确的。
来自您的链接(尽管谈论的是 AsyncTask,而不是匿名 Runnable):
if it is an inner class of your Activity/Fragment, it holds an implicit reference to it, which is bad practice, because Activity/Fragment can be destroyed on configuration change, but they will be kept in memory while worker thread is alive; if it is declared as standalone or static inner class and you are using reference to a Context to update views, you should always check whether it is null or not"
关于java - 匿名可运行对象可防止 Android 对定义它的 Activity 进行垃圾收集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40437863/