On 27 Apr, 13:48, goosedroid <alexrhel...@gmail.com> wrote:
> I have been trying to find a discussion on the best way to handle a
> common sqlite database which is shared by multiple Activities (or
> multiple Activities and Services). This is all in the same
> application.
>
> It seems that if each Activity has:
>
> void onCreate()
> {
>     SQLiteDatabase db = SQLiteDatabase.openDatabase(...)
>     ...
>
> }
>
> void onDestroy()
> {
>    db.close();
>    ...
>
> }
>
> this would be wasteful, assuming there would be N SQLiteDatabase
> objects with N activities that needed access to the same db.
>
> One could go about making a singleton, but who would be responsible
> for closing the database when the application terminates?
>
> Would it be best to extend the android.app.Application class and hook
> into the onTerminate() to close the database?
>
> Taking a look at NotePadv3Solution, I can't find where it closes the
> database - and that is a pretty straightforward example.
>
> --

What I did (which may not be the best solution is to call the
db.close() in onPause() method and call the db.open() in any
onResume() method of all activities.
Be aware that during the activity creation both onCreate() and
onResume() methods are called, so you need to check the db is not
opened yet.

Hope this helps,

    Federico

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to