Shared Preferences

Using the SharedPreferences class you can create named maps of key/value pairs within your application that can be shared among application components running in the same application context.

Shared Preferences support the primitive types Boolean, string, float, long, and integer, making them an ideal means of quickly storing default values, class instance variables, the current UI state, and user preferences. They are most commonly used to persist data across user sessions and to share settings among application components.

Activities also offer the onSaveInstanceState handler. It’s designed specifically to persist UI state when the Activity becomes eligible for termination by a resource-hungry run time.

The handler works like the Shared Preference mechanism. It offers a Bundle parameter that represents a key/value map of primitive types that can be used to save the Activity’s instance values. This Bundle is then made available as a parameter passed in to the onCreate and onRestoreInstanceState method handlers.

This UI state Bundle should be used to record the values needed for an Activity to present an identical UI when it’s displayed after an unexpected close.

CREATING AND SAVING PREFERENCES
// Retrieve an editor to modify the shared preferences.
SharedPreferences.Editor editor = mySharedPreferences.edit();
// Store new primitive types in the shared preferences object.
editor.putBoolean("isTrue", true);
editor.putFloat("lastFloat", 1f);
editor.putInt("wholeNumber", 2);
editor.putLong("aNumber", 3l);
editor.putString("textEntryValue", "Not Empty");
// Commit the changes.
editor.commit();
}

RETRIEVING SHARED PREFERENCES
public static String MY_PREFS = "MY_PREFS";
public void loadPreferences() {
// Get the stored preferences
int mode = Activity.MODE_PRIVATE;
SharedPreferences mySharedPreferences = getSharedPreferences(MY_PREFS, mode);
// Retrieve the saved values.
boolean isTrue = mySharedPreferences.getBoolean("isTrue", false);
float lastFloat = mySharedPreferences.getFloat("lastFloat", 0f);
int wholeNumber = mySharedPreferences.getInt("wholeNumber", 1);
long aNumber = mySharedPreferences.getLong("aNumber", 0);
String stringPreference = mySharedPreferences.getString("textEntryValue", "");
}

The Preference Activity framework consists of three parts:
- Preference Screen Layout: An XML file that defines the hierarchy displayed in your Preference Activity. It specifies the controls to display, the values to allow, and the Shared Preference keys to use for each UI control.
-Preference Activity: An extension of PreferenceActivity that will be used to host your application preference screens.
- Shared Preference Change Listener: An implementation of the onSharedPreferenceChangeListener class used to listen for changes to Shared Preferences.

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="My Preference Category"/>
<CheckBoxPreference android:key="PREF_CHECK_BOX" android:title="Check Box Preference"
android:summary="Check Box Preference Description" android:defaultValue="true" />
</PreferenceCategory>
</PreferenceScreen>

Native Preference Controls:
Android includes several preference controls to build your Preference Screens:
- CheckBoxPreference: A standard preference checkbox control. Used to set preferences to true or false.
- EditTextPreference: Allows users to enter a string value as a preference. Selecting the preference text will display a text entry dialog.
- ListPreference: The preference equivalent of a spinner. Selecting this preference will display a dialog
box containing a list of values from which to select. You can specify different arrays to contain the display text and selection values.
- RingtonePreference: A specialized List Preference that presents the list of available ringtones for user selection. This is particularly useful when you’re constructing a screen to configure notification settings.

5 comments:

  1. Nice...love the logic to save instance. I will use that for incoming calls.

    ReplyDelete
  2. Also see this for saving an activity state:
    http://stackoverflow.com/questions/151777/how-do-i-save-an-android-applications-state

    ReplyDelete
  3. Nice...tnx 4 the post

    ReplyDelete
  4. Simple examples ... thanks.

    ReplyDelete