I'm trying to write an Activity that spawns off an AsyncTask for a long-running operation, and periodically publishes status updates back to the Activity. I'd like to make sure the AsyncTask survives screen rotation or other destruct开发者_如何学Goion conditions like OOM correctly.
I've read this thread on the android-developers list, which in turn led me to this implementation, which is almost what I need. However, the last step of my AsyncTask is to delete some data from a content resolver. It is extremely important that this step is not skipped, because the data is sensitive and should not be left on the phone under normal conditions. However, ContentResolvers come from Contexts, in this case the Activity, and with this scheme the Activity may be null by the time the AsyncTask is ready to do the deletion.
Is it safe to get a ContentResolver from the Activity at AsyncTask construction and hold onto that past the lifetime of the Activity? If not, what can I do to handle this case?
Why don't you get the ContentResolver from the Application ? You need to understand the different concepts behind an Activity and Context - just because an Activity is a Context, doesn't mean that you need to use an Activity for all tasks that it does. Use an Activity only for UI stuff
精彩评论