一个匿名的 runnable 防止 Android 垃圾收集它定义的 activity
An anonymous runnable prevents Android from garbage-collecting the activity in which it's defined
在阅读the example时,我注意到作者在MyActivity
中创建了Runnable task
。如果我关闭 activity,系统将无法对其进行垃圾回收,因为 runnable 包含对 MyActivity 的隐式引用,对吧?只要 runnable 有效,activity 就会存在。
如有错误请指正
似乎正确。
来自你的link(虽然说的是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"
在阅读the example时,我注意到作者在MyActivity
中创建了Runnable task
。如果我关闭 activity,系统将无法对其进行垃圾回收,因为 runnable 包含对 MyActivity 的隐式引用,对吧?只要 runnable 有效,activity 就会存在。
如有错误请指正
似乎正确。
来自你的link(虽然说的是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"