Skip to content

Instantly share code, notes, and snippets.

@alexjlockwood
Created May 23, 2012 01:07
Show Gist options
  • Save alexjlockwood/2772660 to your computer and use it in GitHub Desktop.
Save alexjlockwood/2772660 to your computer and use it in GitHub Desktop.
Singleton Database #2
/**
* create custom DatabaseHelper class that extends SQLiteOpenHelper
*/
public class DatabaseHelper extends SQLiteOpenHelper {
private static DatabaseHelper mInstance = null;
private static final String DATABASE_NAME = "databaseName";
private static final String DATABASE_TABLE = "tableName";
private static final int DATABASE_VERSION = 1;
private Context mCxt;
public static DatabaseHelper getInstance(Context ctx) {
/**
* use the application context, which will ensure that you
* don't accidentally leak an Activity's context.
* (see this article for more information:
* http://developer.android.com/resources/articles/avoiding-memory-leaks.html)
*/
if (mInstance == null) {
mInstance = new DatabaseHelper(ctx.getApplicationContext());
}
return mInstance;
}
/**
* constructor should be private to prevent direct instantiation.
* make call to static factory method "getInstance()" instead.
*/
private DatabaseHelper(Context ctx) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.mCtx = ctx;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment