I've more than one activity (inside the same Application) that needs to have access to the database. What's the best pattern to implement this ? Do I need a content provider even if all activities belong to the same application?
Which activity should have the responsibility for opening开发者_运维问答 and closing the database ?
Your two options are Content Provider or just using your own database abstraction layer. The content provider is a better way to go as pointed out, if you need other apps to share your data or if you need to hook into some other part of Android (like the Quick Search framework). It should not be tied into an Activity - should just be a separate class that you import and use.
The OReilly Android programming book has a chapter which illustrates both approaches, its a good read.
Not necessary. You just have to create a Content Provider if you want some external application to access your data.
Content providers offer a structured storage mechanism that can be limited to your own application or exported to allow access by other applications. If you do not intend to provide other applications with access to your ContentProvider, mark them as android:exported=false in the application manifest. Otherwise, set the android:exported attribute to true to allow other apps to access the stored data.
https://developer.android.com/training/articles/security-tips
精彩评论