PreferenceManager
  public
  
  
  
  class
  PreferenceManager
  
    extends Object
  
  
  
  
  
  
| java.lang.Object | |
| ↳ | android.preference.PreferenceManager | 
      This class was deprecated
      in API level 29.
    Use the AndroidX
      
      Preference Library for consistent behavior across all devices. For more information on
      using the AndroidX Preference Library see
      Settings.
  
Used to help create Preference hierarchies
 from activities or XML.
 
 In most cases, clients should use
 PreferenceActivity.addPreferencesFromIntent or
 PreferenceActivity.addPreferencesFromResource(int).
See also:
Summary
| Nested classes | |
|---|---|
| 
        
        
        
        
        interface | PreferenceManager.OnActivityDestroyListenerThis interface was deprecated in API level 29. Use the AndroidX Preference Library for consistent behavior across all devices. For more information on using the AndroidX Preference Library see Settings. | 
| 
        
        
        
        
        interface | PreferenceManager.OnActivityResultListenerThis interface was deprecated in API level 29. Use the AndroidX Preference Library for consistent behavior across all devices. For more information on using the AndroidX Preference Library see Settings. | 
| 
        
        
        
        
        interface | PreferenceManager.OnActivityStopListenerThis interface was deprecated in API level 29. Use the AndroidX Preference Library for consistent behavior across all devices. For more information on using the AndroidX Preference Library see Settings. | 
| Constants | |
|---|---|
| String | KEY_HAS_SET_DEFAULT_VALUES
 | 
| String | METADATA_KEY_PREFERENCESThe Activity meta-data key for its XML preference hierarchy. | 
| Public methods | |
|---|---|
| 
        
        
        
        
        
        PreferenceScreen | 
      createPreferenceScreen(Context context)
       | 
| 
        
        
        
        
        
        Preference | 
      findPreference(CharSequence key)
      Finds a  | 
| 
        
        
        static
        
        
        SharedPreferences | 
      getDefaultSharedPreferences(Context context)
      Gets a  | 
| 
        
        
        static
        
        
        String | 
      getDefaultSharedPreferencesName(Context context)
      Returns the name used for storing default shared preferences. | 
| 
        
        
        
        
        
        PreferenceDataStore | 
      getPreferenceDataStore()
      Returns the  | 
| 
        
        
        
        
        
        SharedPreferences | 
      getSharedPreferences()
      Gets a  | 
| 
        
        
        
        
        
        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. | 
| 
        
        
        
        
        
        boolean | 
      isStorageDefault()
      Indicates if the storage location used internally by this class is the
 default provided by the hosting  | 
| 
        
        
        
        
        
        boolean | 
      isStorageDeviceProtected()
      Indicates if the storage location used internally by this class is backed by device-protected storage. | 
| 
        
        
        static
        
        
        void | 
      setDefaultValues(Context context, String sharedPreferencesName, int sharedPreferencesMode, int resId, boolean readAgain)
      Similar to  | 
| 
        
        
        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  | 
| 
        
        
        
        
        
        void | 
      setPreferenceDataStore(PreferenceDataStore dataStore)
      Sets a  | 
| 
        
        
        
        
        
        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. | 
| 
        
        
        
        
        
        void | 
      setStorageDefault()
      Sets the storage location used internally by this class to be the default
 provided by the hosting  | 
| 
        
        
        
        
        
        void | 
      setStorageDeviceProtected()
      Explicitly set the storage location used internally by this class to be device-protected storage. | 
| Inherited methods | |
|---|---|
Constants
KEY_HAS_SET_DEFAULT_VALUES
public static final String KEY_HAS_SET_DEFAULT_VALUES
Constant Value: "_has_set_default_values"
METADATA_KEY_PREFERENCES
public static final String METADATA_KEY_PREFERENCES
The Activity meta-data key for its XML preference hierarchy.
Constant Value: "android.preference"
Public methods
createPreferenceScreen
public PreferenceScreen createPreferenceScreen (Context context)
| Parameters | |
|---|---|
| context | Context | 
| Returns | |
|---|---|
| PreferenceScreen | |
findPreference
public Preference findPreference (CharSequence key)
Finds a Preference based on its key.
| Parameters | |
|---|---|
| key | CharSequence: the key of the preference to retrieve | 
| Returns | |
|---|---|
| Preference | the Preferencewith the key, ornull | 
getDefaultSharedPreferences
public 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.
| Parameters | |
|---|---|
| context | Context: The context of the preferences whose values are wanted. | 
| Returns | |
|---|---|
| SharedPreferences | A SharedPreferencesinstance that can be used to retrieve and listen
         to values of the preferences. | 
getDefaultSharedPreferencesName
public static String getDefaultSharedPreferencesName (Context context)
Returns the name used for storing default shared preferences.
| Parameters | |
|---|---|
| context | Context | 
| Returns | |
|---|---|
| String | |
See also:
getPreferenceDataStore
public PreferenceDataStore getPreferenceDataStore ()
Returns the PreferenceDataStore associated with this manager or null if
 the default SharedPreferences are used instead.
| Returns | |
|---|---|
| PreferenceDataStore | The PreferenceDataStoreassociated with this manager ornullif none. | 
getSharedPreferences
public SharedPreferences getSharedPreferences ()
Gets a SharedPreferences instance that preferences managed by this will use.
| Returns | |
|---|---|
| SharedPreferences | a SharedPreferencesinstance pointing to the file that contains the values of
         preferences that are managed by this PreferenceManager. If aPreferenceDataStorehas been set, this method returnsnull. | 
getSharedPreferencesMode
public 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 Context.getSharedPreferences(String, int). | 
getSharedPreferencesName
public 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 Context.getSharedPreferences(String, int). | 
isStorageDefault
public boolean isStorageDefault ()
Indicates if the storage location used internally by this class is the
 default provided by the hosting Context.
| Returns | |
|---|---|
| boolean | |
isStorageDeviceProtected
public boolean isStorageDeviceProtected ()
Indicates if the storage location used internally by this class is backed by device-protected storage.
| Returns | |
|---|---|
| boolean | |
setDefaultValues
public static void setDefaultValues (Context context, String sharedPreferencesName, int sharedPreferencesMode, int resId, boolean readAgain)
Similar to setDefaultValues(android.content.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
 asContext.MODE_PRIVATEorContext.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 theKEY_HAS_SET_DEFAULT_VALUESin the default value shared
 preferences file is false). To attempt to set the default values again
 bypassing this check, setreadAgainto true.
            Note: this will NOT reset preferences back to their default
            values. For that functionality, use
             | 
setDefaultValues
public 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. 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 theKEY_HAS_SET_DEFAULT_VALUESin the default value shared
 preferences file is false). To attempt to set the default values again
 bypassing this check, setreadAgainto true.
            Note: this will NOT reset preferences back to their default
            values. For that functionality, use
             | 
setPreferenceDataStore
public void setPreferenceDataStore (PreferenceDataStore dataStore)
Sets a PreferenceDataStore to be used by all Preferences associated with this manager
 that don't have a custom PreferenceDataStore assigned via
 Preference.setPreferenceDataStore(PreferenceDataStore). Also if the data store is
 set, the child preferences won't use SharedPreferences as long as
 they are assigned to this manager.
| Parameters | |
|---|---|
| dataStore | PreferenceDataStore: ThePreferenceDataStoreto be used by this manager. | 
setSharedPreferencesMode
public 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. | 
setSharedPreferencesName
public void setSharedPreferencesName (String sharedPreferencesName)
Sets the name of the SharedPreferences file that preferences managed by this will use.
If custom PreferenceDataStore is set, this won't override its usage.
| Parameters | |
|---|---|
| sharedPreferencesName | String: The name of the SharedPreferences file. | 
setStorageDefault
public void setStorageDefault ()
Sets the storage location used internally by this class to be the default
 provided by the hosting Context.
setStorageDeviceProtected
public void setStorageDeviceProtected ()
Explicitly set the storage location used internally by this class to be device-protected storage.
On devices with direct boot, data stored in this location is encrypted with a key tied to the physical device, and it can be accessed immediately after the device has booted successfully, both before and after the user has authenticated with their credentials (such as a lock pattern or PIN).
Because device-protected data is available without user authentication, you should carefully limit the data you store using this Context. For example, storing sensitive authentication tokens or passwords in the device-protected area is strongly discouraged.
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2025-02-10 UTC.
