如果我使用的是singelton,那么关闭SQLiteOpenHelper的位置

use*_*o01 3 android

我有以下类允许用户获取SQLiteOpenHelper对象

import android.content.Context;

public class DBUtils {  

    private DBUtils(){

    }


    private static DBHelper dbHelper ;


    public static synchronized DBHelper getDBHelper(Context context){

        if(dbHelper == null){
            dbHelper = new DBHelper(context, ApplicationMetaData.DATABASE_NAME, null, ApplicationMetaData.DATABASE_VERSION);
        }
        return dbHelper;


    }



    public static synchronized void closeDBHelper(){

        if(dbHelper!=null )
            dbHelper.close();
        dbHelper = null;
    }


    @Override
    protected Object clone() throws CloneNotSupportedException {
        // TODO Auto-generated method stub
        throw new CloneNotSupportedException();
    }

}
Run Code Online (Sandbox Code Playgroud)

我想知道我在哪个close单独的对象我不能在onTerminate()方法上中继,因为它不会被调用.我想在用户退出我的应用时关闭它?任何解决方案

Kev*_*gan 6

不要担心关闭它.只需确保您永远不会创建多个SQLiteOpenHelper.看这里:

http://touchlabblog.tumblr.com/post/24474750219/single-sqlite-connection