Search code examples
androidwidgetandroid-contentprovidersharedpreferences

Widget data: ContentProvider or SharedPreferences?


I'm developing a widget, it needs both to store data local to the widget instance and global to all widgets (cache and common data). Currently i'm using a PreferenceActivity setting its sharedpreference file to MYAPPNAME+WIDGETID, then i store common data (4/5 vars) in MYAPPNAME prefs.

I saw a lot of examples on the net using ContentProvider for this purpose, is there any advantage? Is it faster or different in some way to sharedpreferences? Sorry but its really not clear.


Solution

  • My recommendation would be, in order of, er, preference:

    1. SQLite (with or without a content provider)
    2. SharedPreferences

    The reason? Transactions. SharedPreferences are just stored in an XML file. SQLite is transactional. I trust SQLite for data integrity a bit more than an XML file.

    However, you do not need a content provider to manage data in SQLite, though you can use one if you wish.