{"id":1820,"date":"2014-10-27T14:45:06","date_gmt":"2014-10-27T09:15:06","guid":{"rendered":"http:\/\/codetheory.in\/?p=1820"},"modified":"2014-10-27T16:31:12","modified_gmt":"2014-10-27T11:01:12","slug":"android-application-data-storage-sharedpreferences","status":"publish","type":"post","link":"https:\/\/codetheory.in\/android-application-data-storage-sharedpreferences\/","title":{"rendered":"Android Application Data Storage With SharedPreferences"},"content":{"rendered":"

Android has a concept of shared preferences using which application preferences data can be stored persistently. That means the data or state won’t be lost until the application is uninstalled. The preferences data can be stored as key\/value pairs and are available across all the Activities of the given application or can also be restricted to a particular Activity.<\/p>\n

<\/p>\n

SharedPreferences<\/h2>\n

Using the SharedPreferences<\/a> interface implementations we can store persistent sets of data in the filesystem. The data will be available across application restarts or even device stop\/start. Consider it as a small cave to hold your app’s data mostly related to settings and user preferences. Anything that is slightly more complicated like relational data should still go into the sqlite database or media files into the filesystem.<\/p>\n

SharedPreferences can be used to store any (and only) primitive data types – booleans, floats, ints, longs and strings – that’ll persist across user sessions (user closes the app and re-opens it). To determine what type of data to store, just think of anything that might require caching for quick usage like username, logged in state, email, high score or level in a game, gender (or other profile info), app-related settings, etc.<\/p>\n

Shared Preferences can be stored at 2 levels – activity or application. To get a SharedPreferences object for your activity or application in order to start storing, retrieving and updating data there are two methods:<\/p>\n