Kotlin: Contexts & SharedPreferences

Last updated 1 year ago by Mark Allison

kotlin

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