Matt W. Matt W. - 1 year ago 78
Android Question

Ormlite setup without using base activities

I'm using ORMLite in an android project, and I'm not wanting to use the extended activities because I'm inserting values into the database on an AsyncTask.

In the docs it says:

"If you do not want to extend the

and other base classes then you will need to duplicate their functionality. You will need to call
OpenHelperManager.getHelper(Context context, Class openHelperClass)
at the start of your code, save the helper and use it as much as you want, and then call
when you are done with it."

It also says to add the database helper class in the
, which I have. So I'm not sure what I'm doing wrong.

I'm using a class called
for my data tier that looks like this:

public class DataAccess {
private Context context;
private DBHelper dbHelper;

public DataAccess(Context _context) {
this.context = _context;
dbHelper = getDBHelper(_context);

private DBHelper getDBHelper(Context context) {
if (dbHelper == null) {
dbHelper = (DBHelper) OpenHelperManager.getHelper(context, DBHelper.class);
return dbHelper;

And I'm using the extended helper class:

public class DBHelper extends OrmLiteSqliteOpenHelper {
private static final String DATABASE_NAME = "database.db";
private static final int DATABASE_VERSION = 1;

private Dao<SomeObject, Integer> someObjectTable = null;
private ConnectionSource connectionSource = null;

public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);

public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) {
this.connectionSource = connectionSource;
try {
TableUtils.createTable(connectionSource, SomeObject.class);
} catch (SQLException e) {
throw new RuntimeException(e);

public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource, int oldVersion, int newVersion) {

public Dao<SomeObject, Integer> getSomeObjectDao() throws SQLException {
if (someObjectTable == null) {
dateTable = getDao(SomeObject.class);
return someObjectTable;

The idea is to create the
class and have it create the
if it hasn't already.

Can someone tell me if this is right or wrong, or if I'm on the right path?


Answer Source

I'm using ORMLite in an android project, and I'm not wanting to use the extended activities because I'm inserting values into the database on an AsyncTask.

You are on the right track but a little off @Matt. Frankly I'd never done a project without extending our base classes. But it is a good exercise so I've created this ORMLite example project which uses an Activity and manages its own helper.

Your DBHelper class is fine but really you do not need your DataAccess class. In each of your activities (or services...) you will need to have something like the following:

private DBHelper dbHelper = null;

protected void onDestroy() {
    if (dbHelper != null) {
        dbHelper = null;

private DBHelper getHelper() {
    if (dbHelper == null) {
        dbHelper = (DBHelper)OpenHelperManager.getHelper(this, DBHelper.class);
    return dbHelper;

You [obviously], then use this in your code by doing something like:

Dao<SomeObject, Integer> someObjectDao = getHelper().getSomeObjectDao();

So whenever you call getHelper() the first time, it will get the helper through the manager, establishing the connection to the database. Whenever your application gets destroyed by the OS, it will release the helper -- possibly closing the underlying database connection if it is the last release.

Notice that the OpenHelperManager.getHelper() needs the Context as the first argument in case you do this without even an Activity base class.


If you do want to create a DataAccess type class to centralize the handling of the helper class then you will need to make the methods static and do your own usage counter. If there are multiple activities and background tasks calling getHelper() then the question is when do you call releaseHelper()? You'll have to increment a count for each get and only call release when the counter gets back to 0. But even then, I'm not 100% sure how many lines you'd save out of your activity class.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download