Search code examples
androidsqliteandroid-contentprovider

Android: Content provider best practices


I develop an Android app, and I need to use the SQLite database. So I have 2 possibilities to:

  • To use Data Object Layer class: these class will use my Dbhelper and will do CRUD actions.
  • To use ContentProvider

In this last case, should I create several ContentProvider according my different tables impacted in my SQLite database (not 1 COntentProvider for 1 Table, but maybe for a tables group?)

My application is to manage the athlete's sport workout.

Thank you guys!


Solution

  • I think if possible you should always use a ContentProvider. In previous Android versions it was more customary to use a DbHelper or DbManager to do CRUD operations but that has changed. ContentProvider already bring a lot of functionality with them that you wold otherwise have to write yourself and they do a lot of work for you e.g. thread safety etc.

    As for your other question: Usually you only have one ContentProvider per app. Or it should at least be avoided to have more than one if it is possible and it fits into your requirements. One case where I would say more than one is justified would be if you have data which should not be accessible to other apps and other data which should be accessible to other apps.