Skip to content

Instantly share code, notes, and snippets.

@adammagana
Created March 19, 2021 16:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save adammagana/676d38e2b4aa4f5fab2fd98a2ffbf80b to your computer and use it in GitHub Desktop.
Save adammagana/676d38e2b4aa4f5fab2fd98a2ffbf80b to your computer and use it in GitHub Desktop.
An Android SharedPreferences function extension that simplifies the edit-and-apply flow
import android.content.SharedPreferences
/**
* Simple extension to remove the need to call `SharedPreferences.edit()` and `SharedPreference.Editor.apply()` for
* every preferences change.
*/
fun SharedPreferences.update(updateBlock: SharedPreferences.Editor.() -> Unit) {
val editor = edit()
updateBlock(editor)
editor.apply()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment