tags:

views:

93

answers:

2

What is the best way for sharing one SQLite DB between several activities? Tables from DB are shown in ListView, and also deleting/inserting records is to be performed. I heard smth about Services, but did not found example for my problem. Now I have SQLiteOpenHelper class for opening DB. I close db in OnPause() and open it in onResume(). But I can't insert data to db from sub-activity, smth goes wrong.

+1  A: 

Create an Application class for your app. This will remain active in memory for as long as any part of your App is running. You can create your DB from the onCreate method and clean it up in the onTerminate method. (Note that there is no guarantee that onTerminate will be called, so you should be careful about what you depend upon here. However, since a SQLite database is just a file, and is agressively flushed, the close operation is a courtesy more than a necessity.)

You can access the application from any Activity via "getApplication", so the DB will always be available to you.

For more info, see http://developer.android.com/guide/appendix/faq/framework.html#3.

Update:

As requested, here's an example of using getApplication. It's really incredibly simple.

  SQLiteDatabase mDB;
  @Override protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mDB = ((MyApplication)getApplication()).mDB;
  }

If every activity includes this code, then every activity will have its own mDB field which references the same underlying shared DB object.

beekeeper
So, any activity will be able to read/write data without conflicts? Could you show some example for getApplication() metod?
Mighter
All activities, services, etc. use the same event thread, so there's no risk of synchronization problems. You should use "startManagingCursor" for any cursors that you use for adapters within your activities so that they will be deactivated and requeried when you move from one activity to another.
beekeeper
Replacing SQLiteDatabase with SQLiteOpenHelper will be OK?
Mighter
Aye. It is indeed more common to use SQLiteOpenHelper. This approach will work with any sort of shared resource you need. (The one exception would be if you need guaranteed finalization. Since onTerminate isn't guaranteed to be called, you can't really do true sharing while ensuring finalization. You'd instead want to save and recreate the object as you exit and enter each successive activity. Luckily, you don't encounter too many such cases.)
beekeeper
A: 

You could do this implementing a BaseActivity class what is extended by all Activity classes in the application:

public class BaseActivity extends Activity {

    protected static SQLiteOpenHelper database;

    @Override
    protected void onPause() {

            // TODO close database

            super.onPause();
    }

    @Override
    protected void onResume() {

            super.onResume();

            // TODO open database
    }
}




public class OneSubActitivy extends BaseActivity {

    // methods using database from BaseActivity
}
caligari
What is the difference between implementing base activity and implementing onResume and onPause for each activity?
Mighter
It is to implement only once the code in BaseActivity. You must write the common code for all activities in the base class...
caligari