0

I have a class:

public class DbAdapter { private DbHelper dbHelper; private SQLiteDatabase db; private final Context context; ... } 

and i want have it available in all activities. The class provides access to my sqlite database.

What is the most elegant and neat way to do it? I thought about creating object in each activity (it should "connect" me to the same database, right?).

1
  • Haven't really got time to clean this up or paste it as an answer but have a look at my DatabaseHelper pastebin.com/Bgi9dDkP otherwise I'd recommend a Singleton Commented Jul 5, 2011 at 14:17

4 Answers 4

2

You can achieve it extending the Application class. Google has this to say about it:

Base class for those who need to maintain global application state. You can provide your own implementation by specifying its name in your AndroidManifest.xml's tag, which will cause that class to be instantiated for you when the process for your application/package is created.

There is normally no need to subclass Application. In most situation, static singletons can provide the same functionality in a more modular way. If your singleton needs a global context (for example to register broadcast receivers), the function to retrieve it can be given a Context which internally uses Context.getApplicationContext() when first constructing the singleton.

I have done it myself like this:

public class App extends Application { private DbHelper dbHelper; @Override public void onCreate() { super.onCreate(); dbHelper = new DbHelper(this); } public SQLiteDatabase getDatabase(){ return dbHelper.getWritableDatabase(); } @Override public void onTerminate() { super.onTerminate(); dbHelper.close(); } } 

Then you just access it calling getApplicationContext.getDatabase()

Sign up to request clarification or add additional context in comments.

Comments

0

You may take a look at the Singleton Pattern, it gives the opportunity to make a class have only one object created and to make this object accessible from everywhere. Hope this helps.

Comments

0

I think the best way to implement this is to use Service. The Service will keep reference to DbAdapter and all your activities will connect to the Service. When activity is connected to the service it'll keep the reference to it and every time it needs db access it'll use to get the DbAdapter. Using this approach you'll have control over your db connection. If no activities are connected to the service than no one is using the db connection and you can free it.

3 Comments

Can you say something more? I couldn't find any nicely writen how-to. I don't now how to connect to a service from another activity when service is already started.
You can start by reading the documentation about android service. After that you can take a look at default music player source code android.git.kernel.org/?p=platform/packages/apps/…. Pay special attention to MediaPlaybackService and how the activities connect to it. If you want to learn something don't look only for step by step tutorial try to understand what are you doing.
I checked how to do it by following this topic: stackoverflow.com/questions/3433883/… and i get the same error (what a surprise). Do you have any idea hwo to correct it?
0

Have a look at ContentProviders. They're made for accessing data. And regardless of all the emphasis on "sharing data between applications" in the link, they don't have to be public - you can use them as a centralized way to access data in your app.

This blog entry on 'Writing Your Own ContentProvider' shows some code for setting up a ContentProvider that works within one application.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.