Deciding between Activity Context or Application Context to Instantiate SQLiteOpenHelper
Asked Answered
M

2

5

I am new to Android development (to development overall to be frank), and as such I have been reading Commonsware Busy Coder's guide. While reading through the SQLite section, there are a couple things that are not %100 clear to me. Below is what I believe is going on as well as some questions. Please correct me if I am wrong in my thought process.

The author suggests that if you are going to use a database from more than just one Activity in your application, you shouldn't use the Context reference from each Activity to instantiate the SQLiteOpenHelper. Instead, you should use getApplicationContext() provided by Activity.

By doing so, he uses the fact that getApplicationContext() retrieves the singleton instance of Context created soon after the application process begins. Here is where my question arises. I think that if I were to use the Context provided by the Activity(this keyword) when instantiating SQLiteOpenHelper, each Activity will create its own instance of SQLiteOpenHelper. It appears to me that just using the Activity's Context does not inform the Application Context that there already is an instance of the SQLiteHelper created, and hence, it creates a new one instead of re-using the existing one. Is my thinking correct?

I think of Context (and correct me if I am wrong) as the developer's "gateway" to get information and resources provided by the Android OS (i.e. getting hold of a system service via getSystemService()). But doing so using this (from an Activity) or using getApplicationContext() has different implications. Using this, will get an instance of Context which is "local" to your current Activity, but using getApplicationContext() references the whole application. If this is correct, does it mean that when I pass a reference to getApplicationContext() to the SQLiteOpenHelper's constructor it will let my whole Application know that there is an instance of my SQLiteOpenHelper already created. How does SQLiteOpenHelper let the Application know about this? does it use some static method like public/private static dbCreated(Context context){//let context know there is an instance of this running} to let the Application know?

I am sorry if this is confusing.

Thanks ahead of time

Morningglory answered 10/3, 2013 at 22:23 Comment(5)
What is the question?Involuntary
Sorry if it is confusing. The questions are at the end of the third and fourth paragraphs.Morningglory
It sure is. I am looking for a question, that can be answered within the scope of SO. Yours is too broad so far.Involuntary
What I am asking is if my thought process is correct. Why when I use the Activity's own context to instantiate the database helper class Android creates a new instance every time and when I use getApplicationContext() it only creates just one. Sorry for the confusion. This is my first post...Morningglory
cause appContext is single instance for the whole app (like singleton)Hardnett
M
5

I Think by now I do have a better understanding of the concept of Context. I would like to share a link I found that clarifies this topic. http://t.co/9R0bPWiKc5

Morningglory answered 30/6, 2013 at 16:1 Comment(0)
B
2

To your second question, if I understand it correctly, Context is some kind of a Registry of Singletons. So SQLiteOpenHelper is just created and tied to the context given to it. Everything that uses this context to acquire SQLiteOpenHelper will get that tied instance.

Baluchistan answered 24/2, 2014 at 13:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.