[android-developers] Databases and Activity life-cycle

2010-03-26 Thread Anders Widen
Hi!

I've been a long time reader, and I've had problems with handling
databases (open, close, create adapter) within an Activity I think
I've figured out the best way to do it so I wanted to share, by
writing a small blog post to easily handle code. I would love to hear
your thoughts and solutions on the subject.

http://awiden.wordpress.com/2010/03/26/database-mangement-and-the-activity-lifecycle/

-- 
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

To unsubscribe from this group, send email to 
android-developers+unsubscribegooglegroups.com or reply to this email with the 
words REMOVE ME as the subject.


[android-developers] Databases and Activity life-cycle (OnCreate -- onResume)

2010-03-24 Thread Anders Widen
Hi!

How to best handle databases during the Activity life-cyle?

My example:

1. When I start the Activity I want to open the database so I create a
new dbAdapter and open it.

2. onCreate calls populateFields and data is read from the database I
just opened. The database remains opened because other functions in
the Activity needs the database.

3. the Activity gets Paused so I want to close my open database to
release resources : onPause -- dbAdapter.close()

4. the Activity gets Resumed so I need to open the database again:
onResume -- dbAdapter.open()

The problem: This will create two open instances of the database. When
the Activity starts the first time after onCreate has finished
onResume will be called (see: 
http://developer.android.com/intl/fr/images/activity_lifecycle.png).
What's best practise to solve this?

This error of handling databases is also present in the Notepad
Tutorial.

//Anders

-- 
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

To unsubscribe from this group, send email to 
android-developers+unsubscribegooglegroups.com or reply to this email with the 
words REMOVE ME as the subject.