Kotlin extension functions for allowing Parcelable write and read from Android's shared preferences.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Being Location a Parcelable class, and location a valid initialized instance of this class: | |
// For writing | |
sharedPreferences.edit { | |
putParcelable("location", location) | |
} | |
// For reading | |
val loadedLocation = sharedPreferences.getParcelable<Location?>("location", null) | |
// FOR LISTS | |
// Writing | |
sharedPreferences.edit { | |
putParcelableList("location", locations) | |
} | |
// Reading | |
val loadedLocations = sharedPreferences.getParcelableList<Location>("location", listOf()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment