27-Dec-12 (Created: 27-Dec-12) | More in 'Android Data Storage'

Myriad colors of Shared preferences

There are 2 intended purposes for shared preferences in Android. First of these is a persistence mechanism to quickly remember user preferences for android apps. In that context Android has a declarative framework for generating the UI and also the persistence of the values chosen in the UI.

The second use is an extension of the first minus the UI. In this context preferences are merely key/value pairs that are stored by the application at any point of its life with no constraints of the UI.

Inventive programmers have finagled the second usage pattern to store arbitrary java objects as JSON strings and persist them using the preferences key/value pair APIs.

See the ProAndroid series to understand the first two usage patterns well. The book also shows how home screen widget state can be maintained in preferences.

In the upcoming Expert Android book I am going to write about the unintended, but quite useful, json usage pattern using the shared preferences.

This article is a quick rehash of the links and the code snippets that I am collecting which in due time will make their way to the Expert android book.