Google is committed to advancing racial equity for Black communities. See how.

SharedPreferencesView

class SharedPreferencesView
kotlin.Any
   ↳ androidx.datastore.migrations.SharedPreferencesView

Read-only wrapper around SharedPreferences. This will be passed in to your migration. The constructor is public to enable easier testing of migrations.

Summary

Public methods
operator Boolean

Checks whether the preferences contains a preference.

Map<String, Any?>

Retrieve all values from the preferences that are in the specified keySet.

Boolean
getBoolean(key: String, defValue: Boolean)

Retrieves a boolean value from the preferences.

Float
getFloat(key: String, defValue: Float)

Retrieves a float value from the preferences.

Int
getInt(key: String, defValue: Int)

Retrieves a int value from the preferences.

Long
getLong(key: String, defValue: Long)

Retrieves a long value from the preferences.

String?
getString(key: String, defValue: String? = null)

Retrieves a string value from the preferences.

Set<String>?
getStringSet(key: String, defValues: Set<String>? = null)

Retrieves a string set value from the preferences.

Public methods

contains

operator fun contains(key: String): Boolean

Checks whether the preferences contains a preference.

Parameters
key: String the name of the preference to check
Exceptions
IllegalArgumentException if key wasn't specified as part of this migration

getAll

fun getAll(): Map<String, Any?>

Retrieve all values from the preferences that are in the specified keySet.

getBoolean

fun getBoolean(
    key: String,
    defValue: Boolean
): Boolean

Retrieves a boolean value from the preferences.

Parameters
key: String the name of the preference to retrieve
defValue: Boolean value to return if this preference does not exist
Exceptions
IllegalArgumentException if key wasn't specified as part of this migration

getFloat

fun getFloat(
    key: String,
    defValue: Float
): Float

Retrieves a float value from the preferences.

Parameters
key: String the name of the preference to retrieve
defValue: Float value to return if this preference does not exist
Exceptions
IllegalArgumentException if key wasn't specified as part of this migration

getInt

fun getInt(
    key: String,
    defValue: Int
): Int

Retrieves a int value from the preferences.

Parameters
key: String the name of the preference to retrieve
defValue: Int value to return if this preference does not exist
Exceptions
IllegalArgumentException if key wasn't specified as part of this migration

getLong

fun getLong(
    key: String,
    defValue: Long
): Long

Retrieves a long value from the preferences.

Parameters
key: String the name of the preference to retrieve
defValue: Long value to return if this preference does not exist
Exceptions
IllegalArgumentException if key wasn't specified as part of this migration

getString

fun getString(
    key: String,
    defValue: String? = null
): String?

Retrieves a string value from the preferences.

Parameters
key: String the name of the preference to retrieve
defValue: String? = null value to return if this preference does not exist
Exceptions
IllegalArgumentException if key wasn't specified as part of this migration

getStringSet

fun getStringSet(
    key: String,
    defValues: Set<String>? = null
): Set<String>?

Retrieves a string set value from the preferences.

Parameters
key: String the name of the preference to retrieve
defValues: Set<String>? = null value to return if this preference does not exist
Exceptions
IllegalArgumentException if key wasn't specified as part of this migration