I simply want to store my mutable list/array list of Strings to SharedPreferences in order to retrieve the saved list again after re-opening the app (USING GSON).
!! WITHOUT using any class. It's a simple mutable list
(I add things with usedCodesList.add("example")
):
private var usedCodesList = mutableListOf<String>("022", "027")
I did not find any tutorial that fits what I need. (NO Class, NO Recycler View or List View or anything like that. I use usedCodesList.add()
, usedCodesList.clear()
and the Gson library ONLY)
Can someone help me? Any help is GREATLY appreciated!! I'm a beginner and almost dying
CodePudding user response:
It's pretty straightforward, just like any other type.
val editor = sharedPreferences.edit()
editor.putStringSet("my_list", usedCodesList.toSet())
editor.apply() // Note that editor should be used as atomical operation, not have it open all the time.
and
usedCodesList = sharedPrefences.getStringSet("my_list", emptySet())?.toMutableList() ?: mutableListOf()