Android: Update the SQLiteOpenHelper's context property after device rotation
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.
You are leaking an Activity instance 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 Context for your database or create a ContentProvider which has its own context to use.
You should rather use the application context (getApplicationContext()) instead of the activity one (this). If you use the activity context you leak that