Closing the database safely
<p>My application has some Activities and a Service that both read and write from the same SQLite database. To keep them synched up, I use a singleton instance of a SQLiteDatabase object.</p>

<p>However, every once in a while, I get a stack trace through the Android Market console from a customer, and it shows that something tried to access an already closed database.</p>

<p>Currently, my Activities always open and close the database in onCreate and in onPause (if isFinishing).  When an Activity has the database open, it also switches a static boolean to keep my Service from closing it.  My Service (which runs periodically based on an AlarmManager) always opens and immediately closes it once it reads what it needs to. But the service will not close the database if that static boolean from the Activity is on.</p>

<p>I'm not sure where my hole could be, and whether I really need to be closing the database all the time if I have a service periodically using it anyway.</p>
