I’m trying to put some time-consumed database operations into the background thread with the help of AsyncTask. In order to handle the possible device rotation, I save my AsyncTask instance in onRetainNonConfigurationInstance() and reuse them in onCreate(). So far so good…
However since the Activity is recreated after device rotation, I must update all Activity references in my AsyncTask to the new created one. The problem is that, the SQLiteOpenHelper inside the AsyncTask also has a context instance which is passed through its constructor and I just passed the old Activity instance before, now how can I update it to the new Activity?
I have tested, It seems work even I don’t update the context of SQLiteOpenHelper. But I’m still not sure if it is OK.
Thanks
You are leaking an
Activityinstance when you do this, since the garbage collector cannot free it up. Depending on your app, this may or may not be fatal to your memory usage, but it’s not good in any case.You should either use the application
Contextfor your database or create aContentProviderwhich has its own context to use.