to top
Android APIs
public class

PreferenceManager

extends Object
java.lang.Object
   ↳ android.preference.PreferenceManager

Class Overview

Used to help create Preference hierarchies from activities or XML.

In most cases, clients should use addPreferencesFromIntent(Intent) or addPreferencesFromResource(int).

Summary

Nested Classes
interface PreferenceManager.OnActivityDestroyListener Interface definition for a class that will be called when the container's activity is destroyed. 
interface PreferenceManager.OnActivityResultListener Interface definition for a class that will be called when the container's activity receives an activity result. 
interface PreferenceManager.OnActivityStopListener Interface definition for a class that will be called when the container's activity is stopped. 
Constants
String KEY_HAS_SET_DEFAULT_VALUES
String METADATA_KEY_PREFERENCES The Activity meta-data key for its XML preference hierarchy.
Public Methods
PreferenceScreen createPreferenceScreen(Context context)
Preference findPreference(CharSequence key)
Finds a Preference based on its key.
static SharedPreferences getDefaultSharedPreferences(Context context)
Gets a SharedPreferences instance that points to the default file that is used by the preference framework in the given context.
SharedPreferences getSharedPreferences()
Gets a SharedPreferences instance that preferences managed by this will use.
int getSharedPreferencesMode()
Returns the current mode of the SharedPreferences file that preferences managed by this will use.
String getSharedPreferencesName()
Returns the current name of the SharedPreferences file that preferences managed by this will use.
static void setDefaultValues(Context context, String sharedPreferencesName, int sharedPreferencesMode, int resId, boolean readAgain)
Similar to setDefaultValues(Context, int, boolean) but allows the client to provide the filename and mode of the shared preferences file.
static void setDefaultValues(Context context, int resId, boolean readAgain)
Sets the default values from a preference hierarchy in XML.
void setSharedPreferencesMode(int sharedPreferencesMode)
Sets the mode of the SharedPreferences file that preferences managed by this will use.
void setSharedPreferencesName(String sharedPreferencesName)
Sets the name of the SharedPreferences file that preferences managed by this will use.
[Expand]
Inherited Methods
From class java.lang.Object

Constants

public static final String KEY_HAS_SET_DEFAULT_VALUES

Since: API Level 1

Constant Value: "_has_set_default_values"

public static final String METADATA_KEY_PREFERENCES

Since: API Level 1

The Activity meta-data key for its XML preference hierarchy.

Constant Value: "android.preference"

Public Methods

public PreferenceScreen createPreferenceScreen (Context context)

Since: API Level 1

public Preference findPreference (CharSequence key)

Since: API Level 1

Finds a Preference based on its key.

Parameters
key The key of the preference to retrieve.
Returns

public static SharedPreferences getDefaultSharedPreferences (Context context)

Since: API Level 1

Gets a SharedPreferences instance that points to the default file that is used by the preference framework in the given context.

Parameters
context The context of the preferences whose values are wanted.
Returns
  • A SharedPreferences instance that can be used to retrieve and listen to values of the preferences.

public SharedPreferences getSharedPreferences ()

Since: API Level 1

Gets a SharedPreferences instance that preferences managed by this will use.

Returns
  • A SharedPreferences instance pointing to the file that contains the values of preferences that are managed by this.

public int getSharedPreferencesMode ()

Since: API Level 1

Returns the current mode of the SharedPreferences file that preferences managed by this will use.

Returns

public String getSharedPreferencesName ()

Since: API Level 1

Returns the current name of the SharedPreferences file that preferences managed by this will use.

Returns

public static void setDefaultValues (Context context, String sharedPreferencesName, int sharedPreferencesMode, int resId, boolean readAgain)

Since: API Level 1

Similar to setDefaultValues(Context, int, boolean) but allows the client to provide the filename and mode of the shared preferences file.

public static void setDefaultValues (Context context, int resId, boolean readAgain)

Since: API Level 1

Sets the default values from a preference hierarchy in XML. This should be called by the application's main activity.

If readAgain is false, this will only set the default values if this method has never been called in the past (or the KEY_HAS_SET_DEFAULT_VALUES in the default value shared preferences file is false). To attempt to set the default values again bypassing this check, set readAgain to true.

Parameters
context The context of the shared preferences.
resId The resource ID of the preference hierarchy XML file.
readAgain Whether to re-read the default values.

Note: this will NOT reset preferences back to their default values. For that functionality, use getDefaultSharedPreferences(Context) and clear it followed by a call to this method with this parameter set to true.

public void setSharedPreferencesMode (int sharedPreferencesMode)

Since: API Level 1

Sets the mode of the SharedPreferences file that preferences managed by this will use.

Parameters
sharedPreferencesMode The mode of the SharedPreferences file.

public void setSharedPreferencesName (String sharedPreferencesName)

Since: API Level 1

Sets the name of the SharedPreferences file that preferences managed by this will use.

Parameters
sharedPreferencesName The name of the SharedPreferences file.