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.

Thanks

Answers


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


Need Your Help

submitting a POST form calls my document.ready() function again?

javascript html dom post document-ready

I have an index.php page which imports a "myjs.js" file which includes a document.ready() function. Inside the document.ready() function, I show the home tab by calling .hide(); on all the divs that

How to populate my WTForm variables?

python google-app-engine jinja2 wtforms

I'm enabling a function that can edit an entity. I want to populate the form with the variables from the datastore. How can I do it? My code doesn't populate the form:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.