Weakreference get() method how safe is it? (Android, asynctask)

后端 未结 2 1563
你的背包
你的背包 2021-02-06 00:19

I am making an Android mobile app. I have a WeakReference to my Activity in the AsyncTask to ensure that it can be garbage collected.

When onPostExecute() g

2条回答
  •  后悔当初
    2021-02-06 00:42

    It's safe!
    As soon as you assign the result of get() to a variable, you have a strong reference again which blocks gargbage collection for this object as long as the new reference exists.
    Of course, after this assignment you need to check if activity is null.

提交回复
热议问题