tags:

views:

41

answers:

2

Ok so in my application I have a DBAdapter class, that does its job well. In the main Activity, I have a variable this.dbAdapter. I would like to share this variable with other Activites, so I don't have to create a new DBAdapter in each Activity.

How would be the best way of doing this? Should I do something like intent.putExtra("DbAdapter", this.dbAdapter);? Or is there another way that I should go about it?

Thanks!

A: 

I would like to share this variable with other Activites, so I don't have to create a new DBAdapter in each > Activity.

Please don't do this. Adapters are tightly coupled with their activities and underlying data. All you will do is cause your app to crash.

CommonsWare
What would be a good alternative to what I'd like then?
Chiggins
Are you sure it's some kind of CursorAdapter subclass? Or I just didn't understood something?
Nikolay Ivanov
@Chiggins: Just create the new `DBAdapter`.
CommonsWare
+1  A: 

The best solution is to create a Content Provider and have each activity query it, using an Adapter to tie your query results to your widgets. However, you can get by with just creating a new DBAdapter in each Activity, making sure to close all resources when the Activity is paused. If you need to persist state in your Adapter, you're probably doing something wrong.

Steve Pomeroy
By the responses on this page, I'll just go with creating a new DBAdapter on each Activity.
Chiggins