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

# SharedPreferences.Editor

*** ** * ** ***

` public static interface SharedPreferences.Editor `   
` `

|---|
| android.content.SharedPreferences.Editor |

<br />

*** ** * ** ***

Interface used for modifying values in a `https://developer.android.com/reference/android/content/SharedPreferences` object. All changes you make in an editor are batched, and not copied back to the original `https://developer.android.com/reference/android/content/SharedPreferences` until you call `https://developer.android.com/reference/android/content/SharedPreferences.Editor#commit()` or `https://developer.android.com/reference/android/content/SharedPreferences.Editor#apply()`

## Summary

| ### Public methods ||
|---|---|
| ` abstract void` | ` https://developer.android.com/reference/android/content/SharedPreferences.Editor#apply()() ` Commit your preferences changes back from this Editor to the `https://developer.android.com/reference/android/content/SharedPreferences` object it is editing. |
| ` abstract https://developer.android.com/reference/android/content/SharedPreferences.Editor` | ` https://developer.android.com/reference/android/content/SharedPreferences.Editor#clear()() ` Mark in the editor to remove *all* values from the preferences. |
| ` abstract boolean` | ` https://developer.android.com/reference/android/content/SharedPreferences.Editor#commit()() ` Commit your preferences changes back from this Editor to the `https://developer.android.com/reference/android/content/SharedPreferences` object it is editing. |
| ` abstract https://developer.android.com/reference/android/content/SharedPreferences.Editor` | ` https://developer.android.com/reference/android/content/SharedPreferences.Editor#putBoolean(java.lang.String,%20boolean)(https://developer.android.com/reference/java/lang/String key, boolean value) ` Set a boolean value in the preferences editor, to be written back once `https://developer.android.com/reference/android/content/SharedPreferences.Editor#commit()` or `https://developer.android.com/reference/android/content/SharedPreferences.Editor#apply()` are called. |
| ` abstract https://developer.android.com/reference/android/content/SharedPreferences.Editor` | ` https://developer.android.com/reference/android/content/SharedPreferences.Editor#putFloat(java.lang.String,%20float)(https://developer.android.com/reference/java/lang/String key, float value) ` Set a float value in the preferences editor, to be written back once `https://developer.android.com/reference/android/content/SharedPreferences.Editor#commit()` or `https://developer.android.com/reference/android/content/SharedPreferences.Editor#apply()` are called. |
| ` abstract https://developer.android.com/reference/android/content/SharedPreferences.Editor` | ` https://developer.android.com/reference/android/content/SharedPreferences.Editor#putInt(java.lang.String,%20int)(https://developer.android.com/reference/java/lang/String key, int value) ` Set an int value in the preferences editor, to be written back once `https://developer.android.com/reference/android/content/SharedPreferences.Editor#commit()` or `https://developer.android.com/reference/android/content/SharedPreferences.Editor#apply()` are called. |
| ` abstract https://developer.android.com/reference/android/content/SharedPreferences.Editor` | ` https://developer.android.com/reference/android/content/SharedPreferences.Editor#putLong(java.lang.String,%20long)(https://developer.android.com/reference/java/lang/String key, long value) ` Set a long value in the preferences editor, to be written back once `https://developer.android.com/reference/android/content/SharedPreferences.Editor#commit()` or `https://developer.android.com/reference/android/content/SharedPreferences.Editor#apply()` are called. |
| ` abstract https://developer.android.com/reference/android/content/SharedPreferences.Editor` | ` https://developer.android.com/reference/android/content/SharedPreferences.Editor#putString(java.lang.String,%20java.lang.String)(https://developer.android.com/reference/java/lang/String key, https://developer.android.com/reference/java/lang/String value) ` Set a String value in the preferences editor, to be written back once `https://developer.android.com/reference/android/content/SharedPreferences.Editor#commit()` or `https://developer.android.com/reference/android/content/SharedPreferences.Editor#apply()` are called. |
| ` abstract https://developer.android.com/reference/android/content/SharedPreferences.Editor` | ` https://developer.android.com/reference/android/content/SharedPreferences.Editor#putStringSet(java.lang.String,%20java.util.Set<java.lang.String>)(https://developer.android.com/reference/java/lang/String key, https://developer.android.com/reference/java/util/Set<https://developer.android.com/reference/java/lang/String> values) ` Set a set of String values in the preferences editor, to be written back once `https://developer.android.com/reference/android/content/SharedPreferences.Editor#commit()` or `https://developer.android.com/reference/android/content/SharedPreferences.Editor#apply()` is called. |
| ` abstract https://developer.android.com/reference/android/content/SharedPreferences.Editor` | ` https://developer.android.com/reference/android/content/SharedPreferences.Editor#remove(java.lang.String)(https://developer.android.com/reference/java/lang/String key) ` Mark in the editor that a preference value should be removed, which will be done in the actual preferences once `https://developer.android.com/reference/android/content/SharedPreferences.Editor#commit()` is called. |

## Public methods

### apply

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

```
public abstract void apply ()
```

Commit your preferences changes back from this Editor to the `https://developer.android.com/reference/android/content/SharedPreferences` object it is editing. This atomically performs the requested modifications, replacing whatever is currently in the SharedPreferences.

Note that when two editors are modifying preferences at the same time, the last one to call apply wins.

Unlike `https://developer.android.com/reference/android/content/SharedPreferences.Editor#commit()`, which writes its preferences out to persistent storage synchronously, `https://developer.android.com/reference/android/content/SharedPreferences.Editor#apply()` commits its changes to the in-memory `https://developer.android.com/reference/android/content/SharedPreferences` immediately but starts an asynchronous commit to disk and you won't be notified of any failures. If another editor on this `https://developer.android.com/reference/android/content/SharedPreferences` does a regular `https://developer.android.com/reference/android/content/SharedPreferences.Editor#commit()` while a `https://developer.android.com/reference/android/content/SharedPreferences.Editor#apply()` is still outstanding, the `https://developer.android.com/reference/android/content/SharedPreferences.Editor#commit()` will block until all async commits are completed as well as the commit itself.

As `https://developer.android.com/reference/android/content/SharedPreferences` instances are singletons within a process, it's safe to replace any instance of `https://developer.android.com/reference/android/content/SharedPreferences.Editor#commit()` with `https://developer.android.com/reference/android/content/SharedPreferences.Editor#apply()` if you were already ignoring the return value.

**Caution:** The framework ensures in-flight disk writes from `apply()` complete before switching states (such as when Activities or Services start or stop). This means that pending `apply()` calls can block the main thread during component lifecycle transitions, which is a common source of Application Not Responding (ANR) errors.

The SharedPreferences.Editor interface isn't expected to be implemented directly. However, if you previously did implement it and are now getting errors about missing `apply()`, you can simply call `https://developer.android.com/reference/android/content/SharedPreferences.Editor#commit()` from `apply()`.

<br />

### clear

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

```
public abstract SharedPreferences.Editor clear ()
```

Mark in the editor to remove *all* values from the preferences. Once commit is called, the only remaining preferences will be any that you have defined in this editor.

Note that when committing back to the preferences, the clear is done first, regardless of whether you called clear before or after put methods on this editor.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/content/SharedPreferences.Editor` | Returns a reference to the same Editor object, so you can chain put calls together. <br /> |

### commit

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

```
public abstract boolean commit ()
```

Commit your preferences changes back from this Editor to the `https://developer.android.com/reference/android/content/SharedPreferences` object it is editing. This atomically performs the requested modifications, replacing whatever is currently in the SharedPreferences.

Note that when two editors are modifying preferences at the same time, the last one to call commit wins.

If you don't care about the return value and you're using this from your application's main thread, consider using `https://developer.android.com/reference/android/content/SharedPreferences.Editor#apply()` instead. However, note that `apply()` can also cause ANRs during component lifecycle transitions.

<br />

| Returns ||
|---|---|
| `boolean` | Returns true if the new values were successfully written to persistent storage. <br /> |

### putBoolean

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

```
public abstract SharedPreferences.Editor putBoolean (String key, 
                boolean value)
```

Set a boolean value in the preferences editor, to be written back once `https://developer.android.com/reference/android/content/SharedPreferences.Editor#commit()` or `https://developer.android.com/reference/android/content/SharedPreferences.Editor#apply()` are called.

<br />

| Parameters ||
|---|---|
| `key` | `String`: The name of the preference to modify. <br /> |
| `value` | `boolean`: The new value for the preference. <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/content/SharedPreferences.Editor` | Returns a reference to the same Editor object, so you can chain put calls together. <br /> |

### putFloat

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

```
public abstract SharedPreferences.Editor putFloat (String key, 
                float value)
```

Set a float value in the preferences editor, to be written back once `https://developer.android.com/reference/android/content/SharedPreferences.Editor#commit()` or `https://developer.android.com/reference/android/content/SharedPreferences.Editor#apply()` are called.

<br />

| Parameters ||
|---|---|
| `key` | `String`: The name of the preference to modify. <br /> |
| `value` | `float`: The new value for the preference. <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/content/SharedPreferences.Editor` | Returns a reference to the same Editor object, so you can chain put calls together. <br /> |

### putInt

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

```
public abstract SharedPreferences.Editor putInt (String key, 
                int value)
```

Set an int value in the preferences editor, to be written back once `https://developer.android.com/reference/android/content/SharedPreferences.Editor#commit()` or `https://developer.android.com/reference/android/content/SharedPreferences.Editor#apply()` are called.

<br />

| Parameters ||
|---|---|
| `key` | `String`: The name of the preference to modify. <br /> |
| `value` | `int`: The new value for the preference. <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/content/SharedPreferences.Editor` | Returns a reference to the same Editor object, so you can chain put calls together. <br /> |

### putLong

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

```
public abstract SharedPreferences.Editor putLong (String key, 
                long value)
```

Set a long value in the preferences editor, to be written back once `https://developer.android.com/reference/android/content/SharedPreferences.Editor#commit()` or `https://developer.android.com/reference/android/content/SharedPreferences.Editor#apply()` are called.

<br />

| Parameters ||
|---|---|
| `key` | `String`: The name of the preference to modify. <br /> |
| `value` | `long`: The new value for the preference. <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/content/SharedPreferences.Editor` | Returns a reference to the same Editor object, so you can chain put calls together. <br /> |

### putString

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

```
public abstract SharedPreferences.Editor putString (String key, 
                String value)
```

Set a String value in the preferences editor, to be written back once `https://developer.android.com/reference/android/content/SharedPreferences.Editor#commit()` or `https://developer.android.com/reference/android/content/SharedPreferences.Editor#apply()` are called.

<br />

| Parameters ||
|---|---|
| `key` | `String`: The name of the preference to modify. <br /> |
| `value` | `String`: The new value for the preference. Passing `null` for this argument is equivalent to calling `https://developer.android.com/reference/android/content/SharedPreferences.Editor#remove(java.lang.String)` with this key. <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/content/SharedPreferences.Editor` | Returns a reference to the same Editor object, so you can chain put calls together. <br /> |

### putStringSet

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

```
public abstract SharedPreferences.Editor putStringSet (String key, 
                Set<String> values)
```

Set a set of String values in the preferences editor, to be written back once `https://developer.android.com/reference/android/content/SharedPreferences.Editor#commit()` or `https://developer.android.com/reference/android/content/SharedPreferences.Editor#apply()` is called.

<br />

| Parameters ||
|---|---|
| `key` | `String`: The name of the preference to modify. <br /> |
| `values` | `Set`: The set of new values for the preference. Passing `null` for this argument is equivalent to calling `https://developer.android.com/reference/android/content/SharedPreferences.Editor#remove(java.lang.String)` with this key. <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/content/SharedPreferences.Editor` | Returns a reference to the same Editor object, so you can chain put calls together. <br /> |

### remove

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

```
public abstract SharedPreferences.Editor remove (String key)
```

Mark in the editor that a preference value should be removed, which will be done in the actual preferences once `https://developer.android.com/reference/android/content/SharedPreferences.Editor#commit()` is called.

Note that when committing back to the preferences, all removals are done first, regardless of whether you called remove before or after put methods on this editor.

<br />

| Parameters ||
|---|---|
| `key` | `String`: The name of the preference to remove. <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/content/SharedPreferences.Editor` | Returns a reference to the same Editor object, so you can chain put calls together. <br /> |