How long can I hold a ContentResolver in AsyncTask?

I am trying to write an Activity that runs AsyncTask for a long run and periodically posts status updates to the Activity. I would like to make sure that AsyncTask correctly preserves screen rotation or other destruction conditions such as OOM.

I read this thread on the list of Android developers , which in turn led me to this implementation , and that's almost what I need. However, the last step of my AsyncTask is to delete some data from the content converter. It is imperative that this step is not skipped, as the data is sensitive and should not remain on the phone under normal conditions. However, ContentResolvers comes from contexts, in this case, Activity, and with this scheme, Activity may be zero by the time AsyncTask is ready to perform the deletion.

Can I get a ContentResolver from an Activity in AsyncTask and hold it for the duration of the Activity? If not, what can I do to handle this?

+5
source share
1 answer

Why aren't you getting ContentResolver from the app? You need to understand the different concepts behind Activity and Context - just because Activity is a context does not mean that you need to use Activity for all the tasks that it performs. Use activity only for user interface content.

+2
source

All Articles