10

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

2 Answers 2

20

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.

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

7 Comments

So, any activity will be able to read/write data without conflicts? Could you show some example for getApplication() metod?
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.
Replacing SQLiteDatabase with SQLiteOpenHelper will be OK?
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.)
Mighter: I found this article. Yes, you can initialize a global SQLiteOpenHelper handler in the application class. SQLiteOpenHelper's constructor requires a context, which is usually an activity context. In this case, just pass it the application context.
|
3

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
}

2 Comments

What is the difference between implementing base activity and implementing onResume and onPause for each activity?
It is to implement only once the code in BaseActivity. You must write the common code for all activities in the base class...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.