One thing that I’ve noticed other Android developers having trouble with is properly
setting up their SQLiteDatabase
. Often times, I come across questions on StackOverflow
asking about error messages such as,
E/Database(234): Leak found
E/Database(234): Caused by: java.lang.IllegalStateException: SQLiteDatabase created and never closed
As you have probably figured out, this exception is thrown when you have opened more
SQLiteDatabase
instances than you have closed. Managing the database can be complicated
when first starting out with Android development, especially to those who are just beginning
to understand the Activity
lifecycle. The easiest solution is to make your database
instance a singleton instance across the entire application’s lifecycle. This will ensure
that no leaks occur, and will make your life a lot easier since it eliminates the
possibility of forgetting to close your database as you code.