Android – Why Was ContentProvider Created?

androiddesign

The title sums up my question, but to elaborate basically what I want to understand is why the Android designers want apps that need to work with shared data to use a Content Provider rather than just accessing the SQLite database directly?

The only reason I can think of is security because certain files can by accessed only be certain processes and in that way the Content Provider is the gatekeeper that ensures each app has the proper privileges before allowing read and/or write access to the database file. Is that the primary reason why ContentProvider was created?

Best Answer

It is above all a way of insulating data consumers and data providers. You develop your own content provider or extend an existing one if you want to make some of your data public or at least available to other application.

True this can server to control accesses from a security point of view but it also allows you to rework the physical implementation of your data whenever you want. All you need to do is to adapt the back-end of your content provider in that case. The data consumer applications will not have to be rewritten. They will carry on accessing your data through their content resolver unaware of any change in the actual underlying implementation.

Also, Android will instantiate only one instance of your content provider even if the data is accessed by several clients so it will take care of concurrent accesses without you having to care about it.

Finally, I believe it will also handle the drudgery of clean start up and shut down.