Added in API level 1

Editor


interface Editor
android.content.SharedPreferences.Editor

Interface used for modifying values in a SharedPreferences object. All changes you make in an editor are batched, and not copied back to the original SharedPreferences until you call commit or apply

Summary

Public methods
abstract Unit

Commit your preferences changes back from this Editor to the SharedPreferences object it is editing.

abstract SharedPreferences.Editor!

Mark in the editor to remove all values from the preferences.

abstract Boolean

Commit your preferences changes back from this Editor to the SharedPreferences object it is editing.

abstract SharedPreferences.Editor!
putBoolean(key: String!, value: Boolean)

Set a boolean value in the preferences editor, to be written back once commit or apply are called.

abstract SharedPreferences.Editor!
putFloat(key: String!, value: Float)

Set a float value in the preferences editor, to be written back once commit or apply are called.

abstract SharedPreferences.Editor!
putInt(key: String!, value: Int)

Set an int value in the preferences editor, to be written back once commit or apply are called.

abstract SharedPreferences.Editor!
putLong(key: String!, value: Long)

Set a long value in the preferences editor, to be written back once commit or apply are called.

abstract SharedPreferences.Editor!
putString(key: String!, value: String?)

Set a String value in the preferences editor, to be written back once commit or apply are called.

abstract SharedPreferences.Editor!
putStringSet(key: String!, values: MutableSet<String!>?)

Set a set of String values in the preferences editor, to be written back once commit or apply is called.

abstract SharedPreferences.Editor!
remove(key: String!)

Mark in the editor that a preference value should be removed, which will be done in the actual preferences once commit is called.

Public methods

apply

Added in API level 9
abstract fun apply(): Unit

Commit your preferences changes back from this Editor to the 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 commit, which writes its preferences out to persistent storage synchronously, apply commits its changes to the in-memory SharedPreferences immediately but starts an asynchronous commit to disk and you won't be notified of any failures. If another editor on this SharedPreferences does a regular commit while a apply is still outstanding, the commit will block until all async commits are completed as well as the commit itself.

As SharedPreferences instances are singletons within a process, it's safe to replace any instance of commit with apply if you were already ignoring the return value.

You don't need to worry about Android component lifecycles and their interaction with apply() writing to disk. The framework makes sure in-flight disk writes from apply() complete before switching states.

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 commit from apply().

clear

Added in API level 1
abstract fun clear(): SharedPreferences.Editor!

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.

Return
SharedPreferences.Editor! Returns a reference to the same Editor object, so you can chain put calls together.

commit

Added in API level 1
abstract fun commit(): Boolean

Commit your preferences changes back from this Editor to the 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 apply instead.

Return
Boolean Returns true if the new values were successfully written to persistent storage.

putBoolean

Added in API level 1
abstract fun putBoolean(
    key: String!,
    value: Boolean
): SharedPreferences.Editor!

Set a boolean value in the preferences editor, to be written back once commit or apply are called.

Parameters
key String!: The name of the preference to modify.
value Boolean: The new value for the preference.
Return
SharedPreferences.Editor! Returns a reference to the same Editor object, so you can chain put calls together.

putFloat

Added in API level 1
abstract fun putFloat(
    key: String!,
    value: Float
): SharedPreferences.Editor!

Set a float value in the preferences editor, to be written back once commit or apply are called.

Parameters
key String!: The name of the preference to modify.
value Float: The new value for the preference.
Return
SharedPreferences.Editor! Returns a reference to the same Editor object, so you can chain put calls together.

putInt

Added in API level 1
abstract fun putInt(
    key: String!,
    value: Int
): SharedPreferences.Editor!

Set an int value in the preferences editor, to be written back once commit or apply are called.

Parameters
key String!: The name of the preference to modify.
value Int: The new value for the preference.
Return
SharedPreferences.Editor! Returns a reference to the same Editor object, so you can chain put calls together.

putLong

Added in API level 1
abstract fun putLong(
    key: String!,
    value: Long
): SharedPreferences.Editor!

Set a long value in the preferences editor, to be written back once commit or apply are called.

Parameters
key String!: The name of the preference to modify.
value Long: The new value for the preference.
Return
SharedPreferences.Editor! Returns a reference to the same Editor object, so you can chain put calls together.

putString

Added in API level 1
abstract fun putString(
    key: String!,
    value: String?
): SharedPreferences.Editor!

Set a String value in the preferences editor, to be written back once commit or apply are called.

Parameters
key String!: The name of the preference to modify.
value String?: The new value for the preference. Passing null for this argument is equivalent to calling remove(java.lang.String) with this key.
Return
SharedPreferences.Editor! Returns a reference to the same Editor object, so you can chain put calls together.

putStringSet

Added in API level 11
abstract fun putStringSet(
    key: String!,
    values: MutableSet<String!>?
): SharedPreferences.Editor!

Set a set of String values in the preferences editor, to be written back once commit or apply is called.

Parameters
key String!: The name of the preference to modify.
values MutableSet<String!>?: The set of new values for the preference. Passing null for this argument is equivalent to calling remove(java.lang.String) with this key.
Return
SharedPreferences.Editor! Returns a reference to the same Editor object, so you can chain put calls together.

remove

Added in API level 1
abstract fun remove(key: String!): SharedPreferences.Editor!

Mark in the editor that a preference value should be removed, which will be done in the actual preferences once 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.

Parameters
key String!: The name of the preference to remove.
Return
SharedPreferences.Editor! Returns a reference to the same Editor object, so you can chain put calls together.