Last active
February 3, 2023 06:10
-
-
Save ArnyminerZ/580ca9e9c404a172e583b922d453ca78 to your computer and use it in GitHub Desktop.
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