Site is currently on development mode

Kotlin: Contexts & SharedPreferences

Last updated 2 years ago by Mark Allison


The first area worthy of discussion is where we obtain a Context which will store data to device encrypted storage, permitting our app to access it before the user has logged in to the device. To do this in Kotlin we can use an extension function:

fun Context.safeContext(): Context = takeUnless { isDeviceProtectedStorage }?.run { it.applicationContext.let { ContextCompat.createDeviceProtectedStorageContext(it) ?: it } } ?: this

Read full Article