# PreferenceManager

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  
Deprecated in[API level 29](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  
Summary:[Nested Classes](https://developer.android.com/reference/android/preference/PreferenceManager#nestedclasses)\|[Constants](https://developer.android.com/reference/android/preference/PreferenceManager#constants)\|[Methods](https://developer.android.com/reference/android/preference/PreferenceManager#pubmethods)\|[Inherited Methods](https://developer.android.com/reference/android/preference/PreferenceManager#inhmethods)  

# PreferenceManager

*** ** * ** ***

[Kotlin](https://developer.android.com/reference/kotlin/android/preference/PreferenceManager "View this page in Kotlin")\|Java

`
public


class
PreferenceManager
`  
`

extends `[Object](https://developer.android.com/reference/java/lang/Object)`


``


`

|---|--------------------------------------|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||
| ↳ | android.preference.PreferenceManager |

<br />

*** ** * ** ***

<br />

**This class was deprecated in API level 29.**   
Use the[AndroidX](https://developer.android.com/jetpack/androidx)[Preference Library](https://developer.android.com/reference/androidx/preference/package-summary)for consistent behavior across all devices. For more information on using the AndroidX Preference Library see[Settings](https://developer.android.com/guide/topics/ui/settings).

Used to help create[Preference](https://developer.android.com/reference/android/preference/Preference)hierarchies from activities or XML.

In most cases, clients should use[PreferenceActivity.addPreferencesFromIntent](https://developer.android.com/reference/android/preference/PreferenceActivity#addPreferencesFromIntent(android.content.Intent))or[PreferenceActivity.addPreferencesFromResource(int)](https://developer.android.com/reference/android/preference/PreferenceActivity#addPreferencesFromResource(int)).  
**See also:**

- [PreferenceActivity](https://developer.android.com/reference/android/preference/PreferenceActivity)

## Summary

|                                                                                                                                                                                                                                                                           ### Nested classes                                                                                                                                                                                                                                                                            ||
|--------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` interface` | [PreferenceManager.OnActivityDestroyListener](https://developer.android.com/reference/android/preference/PreferenceManager.OnActivityDestroyListener) *This interface was deprecated in API level 29. Use the[AndroidX](https://developer.android.com/jetpack/androidx)[Preference Library](https://developer.android.com/reference/androidx/preference/package-summary)for consistent behavior across all devices. For more information on using the AndroidX Preference Library see[Settings](https://developer.android.com/guide/topics/ui/settings).* |
| ` interface` | [PreferenceManager.OnActivityResultListener](https://developer.android.com/reference/android/preference/PreferenceManager.OnActivityResultListener) *This interface was deprecated in API level 29. Use the[AndroidX](https://developer.android.com/jetpack/androidx)[Preference Library](https://developer.android.com/reference/androidx/preference/package-summary)for consistent behavior across all devices. For more information on using the AndroidX Preference Library see[Settings](https://developer.android.com/guide/topics/ui/settings).*   |
| ` interface` | [PreferenceManager.OnActivityStopListener](https://developer.android.com/reference/android/preference/PreferenceManager.OnActivityStopListener) *This interface was deprecated in API level 29. Use the[AndroidX](https://developer.android.com/jetpack/androidx)[Preference Library](https://developer.android.com/reference/androidx/preference/package-summary)for consistent behavior across all devices. For more information on using the AndroidX Preference Library see[Settings](https://developer.android.com/guide/topics/ui/settings).*       |

|                                                                                                                           ### Constants                                                                                                                            ||
|--------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [String](https://developer.android.com/reference/java/lang/String) | [KEY_HAS_SET_DEFAULT_VALUES](https://developer.android.com/reference/android/preference/PreferenceManager#KEY_HAS_SET_DEFAULT_VALUES) <br />                                                   |
| [String](https://developer.android.com/reference/java/lang/String) | [METADATA_KEY_PREFERENCES](https://developer.android.com/reference/android/preference/PreferenceManager#METADATA_KEY_PREFERENCES) The Activity meta-data key for its XML preference hierarchy. |

|                                                                                                                                                                                                                                                                                                                                                                                                                                                   ### Public methods                                                                                                                                                                                                                                                                                                                                                                                                                                                    ||
|----------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` `[PreferenceScreen](https://developer.android.com/reference/android/preference/PreferenceScreen)       | ` `[createPreferenceScreen](https://developer.android.com/reference/android/preference/PreferenceManager#createPreferenceScreen(android.content.Context))`(`[Context](https://developer.android.com/reference/android/content/Context)` context) `                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
| ` `[Preference](https://developer.android.com/reference/android/preference/Preference)                   | ` `[findPreference](https://developer.android.com/reference/android/preference/PreferenceManager#findPreference(java.lang.CharSequence))`(`[CharSequence](https://developer.android.com/reference/java/lang/CharSequence)` key) ` Finds a[Preference](https://developer.android.com/reference/android/preference/Preference)based on its key.                                                                                                                                                                                                                                                                                                                                                                                                                                                                 |
| ` static `[SharedPreferences](https://developer.android.com/reference/android/content/SharedPreferences) | ` `[getDefaultSharedPreferences](https://developer.android.com/reference/android/preference/PreferenceManager#getDefaultSharedPreferences(android.content.Context))`(`[Context](https://developer.android.com/reference/android/content/Context)` context) ` Gets a[SharedPreferences](https://developer.android.com/reference/android/content/SharedPreferences)instance that points to the default file that is used by the preference framework in the given context.                                                                                                                                                                                                                                                                                                                                      |
| ` static `[String](https://developer.android.com/reference/java/lang/String)                             | ` `[getDefaultSharedPreferencesName](https://developer.android.com/reference/android/preference/PreferenceManager#getDefaultSharedPreferencesName(android.content.Context))`(`[Context](https://developer.android.com/reference/android/content/Context)` context) ` Returns the name used for storing default shared preferences.                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
| ` `[PreferenceDataStore](https://developer.android.com/reference/android/preference/PreferenceDataStore) | ` `[getPreferenceDataStore](https://developer.android.com/reference/android/preference/PreferenceManager#getPreferenceDataStore())`() ` Returns the[PreferenceDataStore](https://developer.android.com/reference/android/preference/PreferenceDataStore)associated with this manager or`null`if the default[SharedPreferences](https://developer.android.com/reference/android/content/SharedPreferences)are used instead.                                                                                                                                                                                                                                                                                                                                                                                    |
| ` `[SharedPreferences](https://developer.android.com/reference/android/content/SharedPreferences)        | ` `[getSharedPreferences](https://developer.android.com/reference/android/preference/PreferenceManager#getSharedPreferences())`() ` Gets a[SharedPreferences](https://developer.android.com/reference/android/content/SharedPreferences)instance that preferences managed by this will use.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
| ` int`                                                                                                   | ` `[getSharedPreferencesMode](https://developer.android.com/reference/android/preference/PreferenceManager#getSharedPreferencesMode())`() ` Returns the current mode of the SharedPreferences file that preferences managed by this will use.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 |
| ` `[String](https://developer.android.com/reference/java/lang/String)                                    | ` `[getSharedPreferencesName](https://developer.android.com/reference/android/preference/PreferenceManager#getSharedPreferencesName())`() ` Returns the current name of the SharedPreferences file that preferences managed by this will use.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 |
| ` boolean`                                                                                               | ` `[isStorageDefault](https://developer.android.com/reference/android/preference/PreferenceManager#isStorageDefault())`() ` Indicates if the storage location used internally by this class is the default provided by the hosting[Context](https://developer.android.com/reference/android/content/Context).                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 |
| ` boolean`                                                                                               | ` `[isStorageDeviceProtected](https://developer.android.com/reference/android/preference/PreferenceManager#isStorageDeviceProtected())`() ` Indicates if the storage location used internally by this class is backed by device-protected storage.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
| ` static void`                                                                                           | ` `[setDefaultValues](https://developer.android.com/reference/android/preference/PreferenceManager#setDefaultValues(android.content.Context,%20java.lang.String,%20int,%20int,%20boolean))`(`[Context](https://developer.android.com/reference/android/content/Context)` context, `[String](https://developer.android.com/reference/java/lang/String)` sharedPreferencesName, int sharedPreferencesMode, int resId, boolean readAgain) ` Similar to[setDefaultValues(android.content.Context, int, boolean)](https://developer.android.com/reference/android/preference/PreferenceManager#setDefaultValues(android.content.Context,%20int,%20boolean))but allows the client to provide the filename and mode of the shared preferences file.                                                                  |
| ` static void`                                                                                           | ` `[setDefaultValues](https://developer.android.com/reference/android/preference/PreferenceManager#setDefaultValues(android.content.Context,%20int,%20boolean))`(`[Context](https://developer.android.com/reference/android/content/Context)` context, int resId, boolean readAgain) ` Sets the default values from an XML preference file by reading the values defined by each[Preference](https://developer.android.com/reference/android/preference/Preference)item's`android:defaultValue`attribute.                                                                                                                                                                                                                                                                                                     |
| ` void`                                                                                                  | ` `[setPreferenceDataStore](https://developer.android.com/reference/android/preference/PreferenceManager#setPreferenceDataStore(android.preference.PreferenceDataStore))`(`[PreferenceDataStore](https://developer.android.com/reference/android/preference/PreferenceDataStore)` dataStore) ` Sets a[PreferenceDataStore](https://developer.android.com/reference/android/preference/PreferenceDataStore)to be used by all Preferences associated with this manager that don't have a custom[PreferenceDataStore](https://developer.android.com/reference/android/preference/PreferenceDataStore)assigned via[Preference.setPreferenceDataStore(PreferenceDataStore)](https://developer.android.com/reference/android/preference/Preference#setPreferenceDataStore(android.preference.PreferenceDataStore)). |
| ` void`                                                                                                  | ` `[setSharedPreferencesMode](https://developer.android.com/reference/android/preference/PreferenceManager#setSharedPreferencesMode(int))`(int sharedPreferencesMode) ` Sets the mode of the SharedPreferences file that preferences managed by this will use.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                |
| ` void`                                                                                                  | ` `[setSharedPreferencesName](https://developer.android.com/reference/android/preference/PreferenceManager#setSharedPreferencesName(java.lang.String))`(`[String](https://developer.android.com/reference/java/lang/String)` sharedPreferencesName) ` Sets the name of the SharedPreferences file that preferences managed by this will use.                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
| ` void`                                                                                                  | ` `[setStorageDefault](https://developer.android.com/reference/android/preference/PreferenceManager#setStorageDefault())`() ` Sets the storage location used internally by this class to be the default provided by the hosting[Context](https://developer.android.com/reference/android/content/Context).                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    |
| ` void`                                                                                                  | ` `[setStorageDeviceProtected](https://developer.android.com/reference/android/preference/PreferenceManager#setStorageDeviceProtected())`() ` Explicitly set the storage location used internally by this class to be device-protected storage.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |

| ### Inherited methods |
|-----------------------|---|
| From class` `[java.lang.Object](https://developer.android.com/reference/java/lang/Object)` ` |--------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` `[Object](https://developer.android.com/reference/java/lang/Object)          | ` `[clone](https://developer.android.com/reference/java/lang/Object#clone())`() ` Creates and returns a copy of this object.                                                                                                                                                      | | ` boolean`                                                                     | ` `[equals](https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object))`(`[Object](https://developer.android.com/reference/java/lang/Object)` obj) ` Indicates whether some other object is "equal to" this one.                                           | | ` void`                                                                        | ` `[finalize](https://developer.android.com/reference/java/lang/Object#finalize())`() ` Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.                                                          | | ` final `[Class](https://developer.android.com/reference/java/lang/Class)`<?>` | ` `[getClass](https://developer.android.com/reference/java/lang/Object#getClass())`() ` Returns the runtime class of this`Object`.                                                                                                                                                | | ` int`                                                                         | ` `[hashCode](https://developer.android.com/reference/java/lang/Object#hashCode())`() ` Returns a hash code value for the object.                                                                                                                                                 | | ` final void`                                                                  | ` `[notify](https://developer.android.com/reference/java/lang/Object#notify())`() ` Wakes up a single thread that is waiting on this object's monitor.                                                                                                                            | | ` final void`                                                                  | ` `[notifyAll](https://developer.android.com/reference/java/lang/Object#notifyAll())`() ` Wakes up all threads that are waiting on this object's monitor.                                                                                                                         | | ` `[String](https://developer.android.com/reference/java/lang/String)          | ` `[toString](https://developer.android.com/reference/java/lang/Object#toString())`() ` Returns a string representation of the object.                                                                                                                                            | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait(long,%20int))`(long timeoutMillis, int nanos) ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*, or until a certain amount of real time has elapsed. | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait(long))`(long timeoutMillis) ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*, or until a certain amount of real time has elapsed.                   | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait())`() ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*.                                                                                             | ||

## Constants

### KEY_HAS_SET_DEFAULT_VALUES

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public static final String KEY_HAS_SET_DEFAULT_VALUES
```

<br />

<br />

Constant Value: "_has_set_default_values"  

### METADATA_KEY_PREFERENCES

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public static final String METADATA_KEY_PREFERENCES
```

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

<br />

Constant Value: "android.preference"

## Public methods

### createPreferenceScreen

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public PreferenceScreen createPreferenceScreen (Context context)
```

<br />

<br />

|         Parameters          ||
|-----------|------------------|
| `context` | `Context` <br /> |

|                                                 Returns                                                 ||
|-------------------------------------------------------------------------------------------------|--------|
| [PreferenceScreen](https://developer.android.com/reference/android/preference/PreferenceScreen) | <br /> |

### findPreference

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public Preference findPreference (CharSequence key)
```

Finds a[Preference](https://developer.android.com/reference/android/preference/Preference)based on its key.

<br />

|                             Parameters                              ||
|-------|--------------------------------------------------------------|
| `key` | `CharSequence`: the key of the preference to retrieve <br /> |

|                                                                                                 Returns                                                                                                  ||
|-------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------|
| [Preference](https://developer.android.com/reference/android/preference/Preference) | the[Preference](https://developer.android.com/reference/android/preference/Preference)with the key, or`null` <br /> |

**See also:**

- [PreferenceGroup.findPreference(CharSequence)](https://developer.android.com/reference/android/preference/PreferenceGroup#findPreference(java.lang.CharSequence))  

### getDefaultSharedPreferences

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public static SharedPreferences getDefaultSharedPreferences (Context context)
```

Gets a[SharedPreferences](https://developer.android.com/reference/android/content/SharedPreferences)instance that points to the default file that is used by the preference framework in the given context.

<br />

|                                      Parameters                                      ||
|-----------|---------------------------------------------------------------------------|
| `context` | `Context`: The context of the preferences whose values are wanted. <br /> |

|                                                                                                                                       Returns                                                                                                                                        ||
|------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [SharedPreferences](https://developer.android.com/reference/android/content/SharedPreferences) | A[SharedPreferences](https://developer.android.com/reference/android/content/SharedPreferences)instance that can be used to retrieve and listen to values of the preferences. <br /> |

### getDefaultSharedPreferencesName

Added in[API level 24](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  
Deprecated in[API level 29](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public static String getDefaultSharedPreferencesName (Context context)
```

Returns the name used for storing default shared preferences.

<br />

|         Parameters          ||
|-----------|------------------|
| `context` | `Context` <br /> |

|                                  Returns                                   ||
|--------------------------------------------------------------------|--------|
| [String](https://developer.android.com/reference/java/lang/String) | <br /> |

**See also:**

- [getDefaultSharedPreferences(Context)](https://developer.android.com/reference/android/preference/PreferenceManager#getDefaultSharedPreferences(android.content.Context))  

### getPreferenceDataStore

Added in[API level 26](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  
Deprecated in[API level 29](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public PreferenceDataStore getPreferenceDataStore ()
```

Returns the[PreferenceDataStore](https://developer.android.com/reference/android/preference/PreferenceDataStore)associated with this manager or`null`if the default[SharedPreferences](https://developer.android.com/reference/android/content/SharedPreferences)are used instead.

<br />

|                                                                                                                               Returns                                                                                                                               ||
|-------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [PreferenceDataStore](https://developer.android.com/reference/android/preference/PreferenceDataStore) | The[PreferenceDataStore](https://developer.android.com/reference/android/preference/PreferenceDataStore)associated with this manager or`null`if none. <br /> |

**See also:**

- [setPreferenceDataStore(PreferenceDataStore)](https://developer.android.com/reference/android/preference/PreferenceManager#setPreferenceDataStore(android.preference.PreferenceDataStore))  

### getSharedPreferences

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public SharedPreferences getSharedPreferences ()
```

Gets a[SharedPreferences](https://developer.android.com/reference/android/content/SharedPreferences)instance that preferences managed by this will use.

<br />

|                                                                                                                                                                                                                                  Returns                                                                                                                                                                                                                                  ||
|------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [SharedPreferences](https://developer.android.com/reference/android/content/SharedPreferences) | a[SharedPreferences](https://developer.android.com/reference/android/content/SharedPreferences)instance pointing to the file that contains the values of preferences that are managed by this PreferenceManager. If a[PreferenceDataStore](https://developer.android.com/reference/android/preference/PreferenceDataStore)has been set, this method returns`null`. <br /> |

### getSharedPreferencesMode

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public int getSharedPreferencesMode ()
```

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

<br />

|                                                                                                 Returns                                                                                                 ||
|-------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `int` | The mode that can be passed to[Context.getSharedPreferences(String, int)](https://developer.android.com/reference/android/content/Context#getSharedPreferences(java.lang.String,%20int)). <br /> |

**See also:**

- [Context.getSharedPreferences(String, int)](https://developer.android.com/reference/android/content/Context#getSharedPreferences(java.lang.String,%20int))  

### getSharedPreferencesName

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public String getSharedPreferencesName ()
```

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

<br />

|                                                                                                                               Returns                                                                                                                                ||
|--------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [String](https://developer.android.com/reference/java/lang/String) | The name that can be passed to[Context.getSharedPreferences(String, int)](https://developer.android.com/reference/android/content/Context#getSharedPreferences(java.lang.String,%20int)). <br /> |

**See also:**

- [Context.getSharedPreferences(String, int)](https://developer.android.com/reference/android/content/Context#getSharedPreferences(java.lang.String,%20int))  

### isStorageDefault

Added in[API level 24](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  
Deprecated in[API level 29](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public boolean isStorageDefault ()
```

Indicates if the storage location used internally by this class is the default provided by the hosting[Context](https://developer.android.com/reference/android/content/Context).

<br />

|      Returns      ||
|-----------|--------|
| `boolean` | <br /> |

**See also:**

- [setStorageDefault()](https://developer.android.com/reference/android/preference/PreferenceManager#setStorageDefault())
- [setStorageDeviceProtected()](https://developer.android.com/reference/android/preference/PreferenceManager#setStorageDeviceProtected())  

### isStorageDeviceProtected

Added in[API level 24](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  
Deprecated in[API level 29](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public boolean isStorageDeviceProtected ()
```

Indicates if the storage location used internally by this class is backed by device-protected storage.

<br />

|      Returns      ||
|-----------|--------|
| `boolean` | <br /> |

**See also:**

- [setStorageDefault()](https://developer.android.com/reference/android/preference/PreferenceManager#setStorageDefault())
- [setStorageDeviceProtected()](https://developer.android.com/reference/android/preference/PreferenceManager#setStorageDeviceProtected())  

### setDefaultValues

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

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

Similar to[setDefaultValues(android.content.Context, int, boolean)](https://developer.android.com/reference/android/preference/PreferenceManager#setDefaultValues(android.content.Context,%20int,%20boolean))but allows the client to provide the filename and mode of the shared preferences file.

<br />

|                                                                                                                                                                                                                                                                                                                                                                                                                         Parameters                                                                                                                                                                                                                                                                                                                                                                                                                          ||
|-------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `context`               | `Context`: The context of the shared preferences. <br />                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
| `sharedPreferencesName` | `String`: A custom name for the shared preferences file. <br />                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    |
| `sharedPreferencesMode` | `int`: The file creation mode for the shared preferences file, such as[Context.MODE_PRIVATE](https://developer.android.com/reference/android/content/Context#MODE_PRIVATE)or[Context.MODE_PRIVATE](https://developer.android.com/reference/android/content/Context#MODE_PRIVATE) <br />                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
| `resId`                 | `int`: The resource ID of the preference XML file. <br />                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |
| `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](https://developer.android.com/reference/android/preference/PreferenceManager#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[PreferenceManager.getDefaultSharedPreferences(Context)](https://developer.android.com/reference/android/preference/PreferenceManager#getDefaultSharedPreferences(android.content.Context))and clear it followed by a call to this method with this parameter set to true. |

**See also:**

- [setDefaultValues(Context, int, boolean)](https://developer.android.com/reference/android/preference/PreferenceManager#setDefaultValues(android.content.Context,%20int,%20boolean))
- [setSharedPreferencesName(String)](https://developer.android.com/reference/android/preference/PreferenceManager#setSharedPreferencesName(java.lang.String))
- [setSharedPreferencesMode(int)](https://developer.android.com/reference/android/preference/PreferenceManager#setSharedPreferencesMode(int))  

### setDefaultValues

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
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](https://developer.android.com/reference/android/preference/Preference)item's`android:defaultValue`attribute. This should be called by the application's main activity.

<br />

<br />

|                                                                                                                                                                                                                                                                                                                                                                                                                 Parameters                                                                                                                                                                                                                                                                                                                                                                                                                  ||
|-------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `context`   | `Context`: The context of the shared preferences. <br />                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       |
| `resId`     | `int`: The resource ID of the preference XML file. <br />                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      |
| `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](https://developer.android.com/reference/android/preference/PreferenceManager#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[PreferenceManager.getDefaultSharedPreferences(Context)](https://developer.android.com/reference/android/preference/PreferenceManager#getDefaultSharedPreferences(android.content.Context))and clear it followed by a call to this method with this parameter set to true. |

### setPreferenceDataStore

Added in[API level 26](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  
Deprecated in[API level 29](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public void setPreferenceDataStore (PreferenceDataStore dataStore)
```

Sets a[PreferenceDataStore](https://developer.android.com/reference/android/preference/PreferenceDataStore)to be used by all Preferences associated with this manager that don't have a custom[PreferenceDataStore](https://developer.android.com/reference/android/preference/PreferenceDataStore)assigned via[Preference.setPreferenceDataStore(PreferenceDataStore)](https://developer.android.com/reference/android/preference/Preference#setPreferenceDataStore(android.preference.PreferenceDataStore)). Also if the data store is set, the child preferences won't use[SharedPreferences](https://developer.android.com/reference/android/content/SharedPreferences)as long as they are assigned to this manager.

<br />

|                                                                                   Parameters                                                                                   ||
|-------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `dataStore` | `PreferenceDataStore`: The[PreferenceDataStore](https://developer.android.com/reference/android/preference/PreferenceDataStore)to be used by this manager. <br /> |

**See also:**

- [Preference.setPreferenceDataStore(PreferenceDataStore)](https://developer.android.com/reference/android/preference/Preference#setPreferenceDataStore(android.preference.PreferenceDataStore))  

### setSharedPreferencesMode

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public void setSharedPreferencesMode (int sharedPreferencesMode)
```

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

<br />

|                                   Parameters                                   ||
|-------------------------|-------------------------------------------------------|
| `sharedPreferencesMode` | `int`: The mode of the SharedPreferences file. <br /> |

**See also:**

- [Context.getSharedPreferences(String, int)](https://developer.android.com/reference/android/content/Context#getSharedPreferences(java.lang.String,%20int))  

### setSharedPreferencesName

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public void setSharedPreferencesName (String sharedPreferencesName)
```

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

If custom[PreferenceDataStore](https://developer.android.com/reference/android/preference/PreferenceDataStore)is set, this won't override its usage.

<br />

|                                    Parameters                                     ||
|-------------------------|----------------------------------------------------------|
| `sharedPreferencesName` | `String`: The name of the SharedPreferences file. <br /> |

**See also:**

- [Context.getSharedPreferences(String, int)](https://developer.android.com/reference/android/content/Context#getSharedPreferences(java.lang.String,%20int))
- [setPreferenceDataStore(PreferenceDataStore)](https://developer.android.com/reference/android/preference/PreferenceManager#setPreferenceDataStore(android.preference.PreferenceDataStore))  

### setStorageDefault

Added in[API level 24](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  
Deprecated in[API level 29](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public void setStorageDefault ()
```

Sets the storage location used internally by this class to be the default provided by the hosting[Context](https://developer.android.com/reference/android/content/Context).

<br />

### setStorageDeviceProtected

Added in[API level 24](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  
Deprecated in[API level 29](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
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.

<br />

**See also:**

- [Context.createDeviceProtectedStorageContext()](https://developer.android.com/reference/android/content/Context#createDeviceProtectedStorageContext())