Working with Application Preferences Android

Many applications need a lightweight data storage mechanism called shared preferences for storing application state, simple user information, configuration options, and other such information.

Android provides a simple preferences system for storing primitive application data at the Activity level and preferences shared across all of an application’s activities. You cannot share preferences outside of the package. Preferences are stored as groups of key/value pairs. The following data types are supported as preference settings:

  • Boolean values
  • Float values
  • Integer values
  • Long values
  • String values

Preference functionality can be found in the SharedPreferences interface of the android. content package. To add preferences support to your application, you must take the following steps:

  1. Retrieve an instance of a SharedPreferences object.
  2. Create a SharedPreferences.Editor to modify preference content.
  3. Make changes to the preferences using the Editor.
  4. Commit your changes.

Creating Private and Shared Preferences

Individual activities can have their own private preferences. These preferences are for the specific Activity only and are not shared with other activities within the application. The activity gets only one group of private preferences.

The following code retrieves the activity’s private preferences:

Creating shared preferences is similar. The only two differences are that we must name our preference set and use a different call to get the preference instance:

You can access shared preferences by name from any activity in the application. There is no limit to the number of different shared preferences you can create. You can have some shared preferences called UserNetworkPreferences and another called AppDisplayPreferences. How you organize shared preferences is up to you, the developer. However, you want to declare your preference name as a variable (in a base class or header) so that you can reuse the name across multiple activities. For example

Searching and Reading Preferences

Reading preferences is straightforward. Simply retrieve the SharedPreferences instance you want to read. You can check for a preference by name, retrieve strongly typed preferences, and register to listen for changes to the preferences. Table describes some helpful methods in the SharedPreferences interface.

Important android.content.SharedPreferences Methods

Important android.content.Shared Preferences Methods

Adding, Updating, and Deleting Preferences

To change preferences, you need to open the preference Editor, make your changes, and commit them. Table describes some helpful methods in the Shared Preferences. Editor interface.

Important android.content.SharedPreferences .Editor Methods

Important android.content.SharedPreferences .Editor Methods

Important android.content.SharedPreferences .Editor Methods

The following block of code retrieves the activity’s private preferences, opens the preference editor, adds a long preference called SomeLong, and saves the change:

Finding Preferences Data on the Android File System

Internally, application preferences are stored as XML files. You can access the preferences file using DDMS using the File Explorer. You find these files on the Android file system in the following directory:

The preferences filename is the Activity’s class name for private preferences or the name you give for the shared preferences. Here is an example of the file contents of a simple preference file with a preference in each data type:

Understanding the application preferences file format can be helpful for testing purposes. You can use Dalvik Debug Monitor Service (DDMS) to copy the preferences files to and from the device.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

Android Topics