android - Database global instance -
So I have to have a database installation for all app activities. I found the following code:
increases the public class MyApplication application {Private Static SQLiteDatabase mDB = null; @ Override Public Empty on Create () {Super. Connet (); Database OpenHelper m_OpenHelper = New Database OpenHelper (this); MDB = m_OpenHelper.getWritableDatabase (); } Public static SQLiteDatabase (getDB) {return MDB; }} I do not understand that when I can close the SQLiteDatabase instance.
This was an issue for me when I first started with Android, because many tutorials on the web There are not those who show how to properly enter your databases in the right way (why not ask me). Here are some sample codes that display three potential approaches.
Approach # 1: Subclassing `Application`
If you know that your application will not be very complicated (i.e. if you know that 'only application' Code> will end with being a sub-class, then you can create subclass of application and extend your main activity, to ensure that an example of a database is used for the whole life cycle Running during. come to the public class main application Increases the amount of daydream / / * DataDbAdapter * For the implementation of personal stability, see Notepad Tutorials: DataDb Adapter MDB Helper; / ** * When the application launches, create a Database Helper * / @ Override Public Wide On Crit () {MDbHelper = New DataDbAdapter (this); mDbHelper.open ();} / ** * Close the Database Helper when the application ends. * / @ Override Public Own On TERMIT () {mDbHelper.close (); MDbHelper = Null;} public static datadab Adapter getDatabaseHelper () {Return MDB Helper;}} Approach # 2: `SQL OpenHelper` is a static data member
This is not a complete implementation, but it You should give a good idea about how to design the class of the databaseHelper , the stable factory system ensures that there is only one database installation available at any time.
/ ** * Create a custom databaseWelpper class which extends SQLiteOpenHelper * / Public class DatabaseHelper Extended SQLiteOpenHelper {Private Static DataShelper with mInstance = Zero; Private Static Last String DATABASE_NAME = "databaseName"; Private static final string DATABASE_TABLE = "tableName"; Personal Static FINAL END DATABASE_VERSION = 1; Personal reference mCxt; Public static databaseHelper hot instances (reference CTX) Use the suggested tips by the commanware. * It will ensure that you do not accidentally leak any activity * reference (see this article for more information: * http://developer.android.com/resources/articles/avoiding-memory-leaks.html) * / If (mInstance = = faucet) {mInstance = New Database Helper (ctx.getApplicationContext ()); } Return M Instance; } / ** * The constructor should be private to stop the institute directly. * Instead of the stationary factory method call "GetInstance ()" * / Personal DatabaseHelper (reference CTX) {super (references, database ANNN, blank, databasebaseSSns); This.mctx = ctx; }} Approach # 3: The essence of the SQLite database with `ContentProvider`
This is the method that I would suggest, the new LoaderManager Classes depend heavily on ContentProviders, so if you use LoaderManager.LoaderCallbacks & lt; Cursor & gt; If you want any activity or piece to implement (which I suggest you take advantage of, it is magical!), You have to implement a ContentProvider for your application Besides, you do not have to worry about creating a singleton database assistant with ContentProviders. Just call getContentResolver () and the system will take care of everything for you (in other words, there is no need to design a singleton pattern to prevent multiple instances). Hope that helps!
Comments
Post a Comment