SharedPreferences内のデータを永続化することは非常に簡単です。あなたのSharedPreferencesで
class UserProfile {
@SerializedName("name") var name: String = ""
@SerializedName("email") var email: String = ""
@SerializedName("age") var age: Int = 10
}
と最終的に私はそのsolitionについて考えたが、私は必要な
fun saveUserProfile(userProfile: UserProfile?) {
val serializedUser = gson.toJson(userProfile)
sharedPreferences.edit().putString(USER_PROFILE, serializedUser).apply()
}
fun readUserProfile(): UserProfile? {
val serializedUser = sharedPreferences.getString(USER_PROFILE, null)
return gson.fromJson(serializedUser, UserProfile::class.java)
}
その場合は、データクラスを使用しないほうがいいですか? – jdtorregrosas
まあ、kotlinでは 'data class'をデフォルト値で使うことができますが、このようにしておくのは良い方法です。それはより読みやすいです。 –