Skip to content

Most visited

Recently visited

navigation
Added in API level 1

PreferenceManager

public class PreferenceManager
extends Object

java.lang.Object
   ↳ android.preference.PreferenceManager


Used to help create Preference hierarchies from activities or XML.

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

See also:

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 an XML preference file by reading the values defined by each Preference item's android:defaultValue attribute.

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.

Inherited methods

From class java.lang.Object

Constants

KEY_HAS_SET_DEFAULT_VALUES

Added in API level 1
String KEY_HAS_SET_DEFAULT_VALUES

Constant Value: "_has_set_default_values"

METADATA_KEY_PREFERENCES

Added in API level 1
String METADATA_KEY_PREFERENCES

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

Constant Value: "android.preference"

Public methods

createPreferenceScreen

Added in API level 1
PreferenceScreen createPreferenceScreen (Context context)

Parameters
context Context
Returns
PreferenceScreen

findPreference

Added in API level 1
Preference findPreference (CharSequence key)

Finds a Preference based on its key.

Parameters
key CharSequence: The key of the preference to retrieve.
Returns
Preference The Preference with the key, or null.

See also:

getDefaultSharedPreferences

Added in API level 1
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.

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

getSharedPreferences

Added in API level 1
SharedPreferences getSharedPreferences ()

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

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

getSharedPreferencesMode

Added in API level 1
int getSharedPreferencesMode ()

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

Returns
int The mode that can be passed to getSharedPreferences(String, int).

See also:

getSharedPreferencesName

Added in API level 1
String getSharedPreferencesName ()

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

Returns
String The name that can be passed to getSharedPreferences(String, int).

See also:

setDefaultValues

Added in API level 1
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.

Parameters
context Context: The context of the shared preferences.
sharedPreferencesName String: A custom name for the shared preferences file.
sharedPreferencesMode int: The file creation mode for the shared preferences file, such as MODE_PRIVATE or MODE_PRIVATE
resId int: The resource ID of the preference XML file.
readAgain boolean: Whether to re-read the default values. If false, this method will set the default values only if this method has never been called in the past (or if 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.

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.

See also:

setDefaultValues

Added in API level 1
void setDefaultValues (Context context, 
                int resId, 
                boolean readAgain)

Sets the default values from an XML preference file by reading the values defined by each Preference item's android:defaultValue attribute. This should be called by the application's main activity.

Parameters
context Context: The context of the shared preferences.
resId int: The resource ID of the preference XML file.
readAgain boolean: Whether to re-read the default values. If false, this method sets the default values only if this method has never been called in the past (or if 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.

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.

setSharedPreferencesMode

Added in API level 1
void setSharedPreferencesMode (int sharedPreferencesMode)

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

Parameters
sharedPreferencesMode int: The mode of the SharedPreferences file.

See also:

setSharedPreferencesName

Added in API level 1
void setSharedPreferencesName (String sharedPreferencesName)

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

Parameters
sharedPreferencesName String: The name of the SharedPreferences file.

See also:

This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.