library / org.akop.kotx.ext.content / android.content.SharedPreferences / getValue

getValue

inline operator fun <reified V> SharedPreferences.getValue(thisRef: Any?, property: KProperty<*>): V?

Allows reading of SharedPreferences setting via delegated property.

Note that using the property on any type not supported by SharedPreferences natively will generate an error.

Example:

var color: String? by Preferences.sharedPrefs
// Corresponds to Preferences.sharedPrefs.getString("color", "red")
val colorWithDefault: String?
    get() = color ?: "red"

Log.v(LOG_TAG, "color: $colorWithDefault")
color = "blue"
Log.v(LOG_TAG, "color: $colorWithDefault")

// Outputs:
// color: red
// color: blue