Search code examples
androidsqliteopenhelper

Where to close SQLiteOpenHelper when opened in my application's onCreate?


I read that it was better to use only one SQLiteOpenHelper instance in a class extending Application:

public class MyApplication extends Application {

    private static SQLiteOpenHelper dBHelper;

    @Override
    public void onCreate() {
        super.onCreate();
        dBHelper = new DBHelper(this);
    }

    public static SQLiteDatabase getDB() {
        return dBHelper.getWritableDatabase();
    }
}

but in that case, where should I close it?

Thanks

Jul


Solution

  • You should always have one and only one SQLOpenHelper instance and you should never close it. More detailed explanation can be found here and here