42
loading...
This website collects cookies to deliver better user experience
@Deprecated
.android.preference.PreferenceManager
has been around since API level 1.val prefs = PreferenceManager.getDefaultSharedPreferences(this)
val current = prefs.getBoolean("hasBeenSet", false)
println(current)
prefs.edit().putBoolean("hasBeenSet", !current).apply()
androidx.preference
. As you will see shortly, it is straightforward to use in most cases. The first step is to include the library in your build.gradle:implementation 'androidx.preference:preference-ktx:1.1.1'
import
statement to androidx.preference.PreferenceManager
the above example works without further changes. But there is more to preferences, for example the integration in the user interface of an app. android.preference.CheckBoxPreference
, PreferenceScreen
and so on) have been deprecated as well, so we need to use the replacements provided by Jetpack Preference. The migration of basic preferences types is simple, yet you may face some effort when you have custom classes. I am not going into detail here, though. Because while this chapter might be closed (we do have a new king now) on other platforms, on Android it is not. Allow me to present...Safely manage keys and encrypt files and sharedpreferences.
implementation 'androidx.security:security-crypto:1.1.0-alpha03'
val masterKey = MasterKey.Builder(this)
.setKeyScheme(MasterKey.KeyScheme.AES256_GCM)
.build()
val prefs = EncryptedSharedPreferences.create(this,
"secret_shared_prefs",
masterKey,
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
)
EncryptedSharedPreferences.create()
. The important thing is: afterwards you can access preferences in the same way like I showed in the beginning, using getBoolean()
, edit()
and putBoolean()
.Welcome Jetpack DataStore, now in alpha - a new and
improved data storage solution aimed at replacing
SharedPreferences. Built on Kotlin coroutines and Flow,
DataStore provides two different implementations: Proto
DataStore, that lets you store typed objects (backed
by protocol buffers) and Preferences DataStore, that stores
key-value pairs. Data is stored asynchronously,
consistently, and transactionally, overcoming most of the
drawbacks of SharedPreferences.
implementation "androidx.datastore:datastore-preferences:1.0.0-rc02"
Use the property delegate created by preferencesDataStore
to create an instance of Datastore<Preferences>
. Call it
once at the top level of your kotlin file, and access it
through this property throughout the rest of your
application. This makes it easier to keep your DataStore
as a singleton.
val Context.dataStore by preferencesDataStore("user_preferences")
val key = booleanPreferencesKey("hasBeenSet")
val flow: Flow<Boolean> = dataStore.data
.map { currentPreferences ->
currentPreferences[key] ?: false
}
lifecycleScope.launch {
println(flow.first())
dataStore.edit { settings ->
val currentCounterValue = settings[key] ?: false
settings[key] = !currentCounterValue
}