Android - How to use ORMLite dbhelper for the entire application life cycle

I would like to use ORMLite, but I do not like that I manage 1 database assistant for each action. Wouldn't it be better to have one for the entire application life cycle? So far I have used greendao, and that is not a problem.

I wanted to implement it, however, I cannot find a suitable way to correctly release the db helper when destroying the application (I can easily open it in Application onCreate), because there is no onDestroy () application method.

Does anyone have a way to do this?

+4
source share
2 answers

Ormlite OrmLiteBaseActivity, . , , . ,   onDestroy() .

, , SherlockActivity ActionBarActivity, , OrmLiteBaseActivity , .

, , . , ! P/s: Android OrmLiteBaseService

+1

Singleton:

package com.example.stackoverflowsandbox;

public class MySingletonHelper extends OrmLiteSqliteOpenHelper {
    private static MySingletonHelper instance;

    public static MySingletonHelper getInstance() {
        if ( MySingletonHelper.instance == null ) {
            MySingletonHelper.instance = new MySingletonHelper();
        }

        return MySingletonHelper.instance;
    }

    private MySingletonHelper() {
        // code here...
    }

    @Override
    public void onCreate( SQLiteDatabase arg0, ConnectionSource arg1 ) {
        // code here...
    }

    @Override
    public void onUpgrade( SQLiteDatabase arg0, ConnectionSource arg1, int arg2, int arg3 ) {
        // code here...
    }
}

: http://en.wikipedia.org/wiki/Singleton_pattern

0

All Articles