Search code examples
androidmemory-leaksandroid-asynctaskandroid-contentresolver

Is it safe to keep a reference to a ContentResolver in an AsyncTask without leaking memory?


I have an AsyncTask in which I'm running a database query. For this purpose my AsyncTask subclass contains a field of the type ContentResolver. I'm wondering if it's safe to have it or if it will cause memory leaks like in the case of a Context (AsyncTask and Contexts)?


Solution

  • From here I could see that ContentResolver keeps a reference to the Context which created it (I guess), so you should take care of it like with any Context references.

    But anw you can always use a ContentResolver from the ApplicationContext, it's said to be safe to use.

    Edit: Or maybe you don't need to store a ContentResolver at all. You can always create one from an existing Context by this constructor. (That constructor even doesn't need a Context, just pass null and it will automatically use the ApplicationContext)