I am new to Android development (in general, to be frank), and so I read the Commonsware Busy Coder manual. When reading through a SQLite section, there are several things that are not 100% for me. The following is what I believe is ongoing, as well as some questions. Please correct me if I am mistaken in the process of my thought.
The author suggests that if you intend to use a database of more than one Activity in your application, you should not use the Context link from each Activity to create an instance of SQLiteOpenHelper . Instead, you should use the getApplicationContext()one provided by Activity.
Thus, it takes advantage of the fact that it getApplicationContext()retrieves the Singleton Context instance created shortly after the start of the application process. This is where my question arises. I think that if I used the Context provided by the Activity (this keyword) when creating an instance of SQLiteOpenHelper , each Activity would create its own instance of SQLiteOpenHelper . It seems to me that just using the activity context does not tell the application context that an instance of the created SQLiteHelper has already been created, and therefore it creates a new one, and not reuses the existing one. Am I correct?
I think of Context (and correct if I am mistaken) as a "gateway" of the developer, in order to receive information and resources provided by the Android OS (that is, to receive a system service through getSystemService()). But using this (from an Activity) or using it getApplicationContext()has different consequences. Using this, you will get a context instance that is "local" to your current action, but using it getApplicationContext(), it refers to the entire application. If this is correct, does this mean that when I pass the reference to the SQLiteOpenHelpergetApplicationContext() constructor , this will let my entire application know that there is an instance of my SQLiteOpenHelper already created. Like SQLiteOpenHelperreports this program? Does it use some static method such as public / private static dbCreated (context context) {// lets the context know if there is an instance of this run} to tell Application?
I apologize if this is confusing.
Thanks in advance
source
share