Use parcelable to store item as sharedpreferences?
Since parcelable doesn't help to place your data in persistent storage (see StenSoft's answer), you can use gson to persist your Location instead:
Saving a Location:
val json = Gson().toJson(location)sharedPreferences.edit().putString("location", json).apply()
Retrieving a Location:
val json = sharedPreferences.getString("location", null)return Gson().fromJson(json, Location::class.java)
In case you're still using Java, replace val
with String
, Gson()
with new Gson()
, ::class.java
with .class
and end each line with a semicolumn.
From documentation of Parcel:
Parcel is not a general-purpose serialization mechanism. This class (and the corresponding Parcelable API for placing arbitrary objects into a Parcel) is designed as a high-performance IPC transport. As such, it is not appropriate to place any Parcel data in to persistent storage: changes in the underlying implementation of any of the data in the Parcel can render older data unreadable.
If you are using Kotlin, I would take the approach of Cristan, but with some extension functions, see:
import android.content.SharedPreferencesimport android.os.Parcelableimport com.google.gson.Gsonimport com.google.gson.JsonSyntaxExceptionfun SharedPreferences.Editor.putParcelable(key: String, parcelable: Parcelable) { val json = Gson().toJson(parcelable) putString(key, json)}inline fun <reified T : Parcelable?> SharedPreferences.getParcelable(key: String, default: T): T { val json = getString(key, null) return try { if (json != null) Gson().fromJson(json, T::class.java) else default } catch (_: JsonSyntaxException) { default }}
And then you could use it as follows, for storing:
sharedPreferences.edit { putParcelable("location", location)}
And for reading:
val location = sharedPreferences.getParcelable<Location?>("location", null)
This is a pretty clean way of using the Cristan proposal. Hope it works for you :)