我有以下允许用户获取 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();
}
}
我想知道我应该在哪里关闭
这个单例对象我不能依赖onTerminate()
方法,因为它不会被调用。我想在用户退出我的应用程序时关闭它?对此的任何解决方案
最佳答案
不用担心关闭它。只要确保您永远不会创建多个 SQLiteOpenHelper。看这里:
http://touchlabblog.tumblr.com/post/24474750219/single-sqlite-connection
关于android - 如果我使用单例,在哪里关闭 SQLiteOpenHelper,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10371233/