Best practices to query SQLite database in ListFragment with CursorLoader?

Sorry no experience in CursorLoader yet and Fragment, but I already experienced use of SQLiteOpenHelper in the context of concurrent access by different threads & activities.

I will assume that PlayersDBAdapter is internally using a SQLiteOpenHelper class. but it is not clear what your methods open() and close() are doing?

What I did:

  • define your SQLiteOpenHelper as an application wide singleton, not activity wide as you seem to do
  • instantiate SQLiteOpenHelper single instance in your Application onCreate
  • DON’T release SQLiteOpenHelper instance in any activity onDestroy, as when an activity stops, another one might still have to open the DB
  • I guess SQLiteOpenHelper instance should be cleared in application onTerminate (not sure as onTerminate is in practice almost never called)
  • I have DBAdapter object which gets a SQLiteDatabase reference with mySQLiteOpenHelper.getWritableDatabase()
  • these DBAdapter are typically allocated in activity onCreate and released in onDestroy

At least this works, no crashs in an application with several thousands users.
Suggestions to improve that are welcome 🙂

Leave a Comment