tags:

views:

103

answers:

2

I want to use a singleton pattern to hold a database and some other data/methods for my Android application.

I realize there are many reasons against singletons, but for this case I'd like to use it.

I've sub-classed UIApplication and made a data class within it called MyAppData.

MyAppData needs to have access to the SQLite database.

When I create the databse, I need to pass a context. I could pass the application context, but it will not directly relate to MyAppData.

I don't know if this wlll cause problems with my code.

So my thought is to have MyAppdata extend android.content.ContextWrapper. I don't think I should extend Activity because it's really not an activity, its a data class with methods to access the database.

I'm wondering if I extend ContextWrapper will there be something deep in the code I'm missing that will cause big problems down the road (memory leaks, etc).

This may not be the ideal approach to this (and I've considered other options), but my goal is to:

Have a singleton class in UIApplication that can encapsulate the database and be retrieved easily from any activity in my app.

Thanks in advance for your suggestions/warnings/advice.

A: 

I've used this approach:

Create a class responsible for managing the db, let's call it DBUtil. This class will extend android.database.sqlite.SQLiteOpenHelper. You can pass a reference to the application context to the constructor of this class. This class will contain methods for creating the db, adding, removing and retrieving items.

Create another class, let's call it AppCore, create a static instance of the DBUtil and a static init() method that accepts an ApplicationContext object

public class AppCore
{
    public static var dbUtil:DBUtil;

    public static void init( ApplicationContext context )
    {
         dbUtil = new DBUtil( context );
    }
}

Then in the onCreate() method of our your application's main Activity, initialize the AppCore class.

@Override
protected void onCreate(Bundle savedInstanceState) 
{
     AppCore.init( getApplicationContext() );   
}

So, it's not really a Singleton. Instead, the DBUtil instance is maintained as a static property, yet still accessible throughout your application, such as this:

AppCore.dbUtil.createNewRecord( params );

Also, I found this tutorial to be very helpful when getting started with this topic: http://developer.android.com/guide/tutorials/notepad/index.html

jeremynealbrown
just_another_coder
This is just my preferred approach. I've used it in multiple projects and it has worked out well. I would recommend extending SQLiteOpenHelper though as it has some convenient hooks. http://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html
jeremynealbrown
oooh wow! thank you Jeremy!
Jorgesys
+1  A: 

Subclass android.database.sqlite.SQLiteOpenHelper and android.app.Application (with the latter being properly declared in AndroidManifest.xml).
Now,

public class MyApplication extends Application {

    private static SQLiteOpenHelper openHelper;

    @Override
    public void onCreate() {
        super.onCreate();
        openHelper = new DbManager(this);
        //
    }

    public static SQLiteDatabase getDB() {
        return openHelper.getWritableDatabase();
    }
}

Then have helper DAO classes that will perform instertions/updates/etc.
That's what I'm using in all of my apps.

alex
I've gone with a similar method, but read this one afterwards.
just_another_coder