ITKeyword,专注技术干货聚合推荐

注册 | 登录

解决Android database helper singleton - instantiate in activity or all fragments?

itPublisher 分享于

2021腾讯云限时秒杀,爆款1核2G云服务器298元/3年!(领取2860元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1062

2021阿里云最低价产品入口+领取代金券(老用户3折起),
入口地址https://www.aliyun.com/minisite/goods

推荐:android Fragments详解五:与activity通讯

与activity通讯   尽管fragment的实现是独立于activity的,可以被用于多个activity,但是每个activity所包含的是同一个fragment的不同的实例。   Fragment可

I have a DBHelper class set up as a singleton:

public class DBHelper extends SQLiteOpenHelper {

    private static DBHelper sInstance;

    public static synchronized DBHelper getInstance(Context context) {
        if (sInstance == null) {
            sInstance = new DBHelper(context.getApplicationContext());
        }
        return sInstance;
    }

    private DBHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        databasePath = context.getDatabasePath(DATABASE_NAME).getPath();
    }
}

I have a MainActivity and a number of fragments. Many of these fragments need access to my DBHelper methods

Should I be using dbHelper = DBHelper.getInstance(getApplicationContext()) in every fragment that needs database access? Instantiation will only happen once due to the singleton pattern, so I don't need to worry about the class being instantiated in every single fragment with that code

Or is it better to instantiate the DBHelper in MainActivity only, and then in any fragment that needs database access get a reference to the mainactivity and call the object methods from there? Something like this in each fragment:

mainActivity = (MainActivity) getActivity();
mainActivity.dbHelper.insertData();
android android-fragments android-activity android-sqlite android-database
|
  this question
asked Apr 4 '16 at 9:26 Simon 2,167 1 19 42 1   The first way is suggested.DB module should be indenpendent –  grantonzhunag Apr 4 '16 at 9:37 1   BTW, you can instantiate the DBHelper in your custom Application which extends Application. –  grantonzhunag Apr 4 '16 at 9:38

 | 

3 Answers
3

解决方法

Since you are sure Singleton will be instantiated in MainActivity the first approach shouldn't have any problems, you could even call getInstance(null) in your fragments


|
  this answer
answered Apr 4 '16 at 9:54 loukaspd 45 6

 | 

I think the most prodcutive decision will be create custom fragment class, extend it your fragment or v4.fragment, initialise in it dbHelper and use your custom fragment in your activity. it is my humble opinion :)

推荐:android database sqlite的使用

首先 系统有个类叫SQLiteOpenHelper 现在知道知道 这个是帮你打开数据库就可以了 自己实现个 public class DatabaseHelper extends SQLiteOpenHelper { priva


|
  this answer
answered Apr 4 '16 at 9:38 Scrobot 90 8

 | 

Codes here is one simple way to resolve concurrent problem in singleton pattern.

public DBHelper extends SQLiteOpenHelper {
    // declare private constructor
    // some public method

    public static class Wrapper {
        private static DBHelper dbHelper;
        public static void init(Context ctx, Object otherArgs) {
            // init DBHelper
            dbHelper = new DBHelper(ctx, otherArgs);
        }
        public static DBHelper get(){
            return dbHelper;
        }
    }
}

In custom Application

public MyApp extends Application{
    void onCreate(){
        DBHelper.Wrpper.init(this, otherArgs);
    }
}

Code like this where DBHelper is needed:

DBHelper.Wrapper.get().insertData();

|
  this answer
answered Apr 4 '16 at 9:53 grantonzhunag 127 5

 | 

推荐:Android SQLite Database Tutorial

表名: 列(字段): 联系人实体类:构造方法,setters 、getters方法 File:   Contact.java package com.example.sqlitetest;public class Contact { int


相关阅读排行


相关内容推荐

最新文章

×

×

请激活账号

为了能正常使用评论、编辑功能及以后陆续为用户提供的其他产品,请激活账号。

您的注册邮箱: 修改

重新发送激活邮件 进入我的邮箱

如果您没有收到激活邮件,请注意检查垃圾箱。