I have an app which saves a handful of lists for a to do list
In android studio, the data saves and loads fine on a "medium phone api 35" type emulator, the ones that are just archetypes of a phone.
but running it on my real phone, or on A API 21 pixel emulator, the data does not save or load
both are lower Apis than the medium api 35, I think my phone is API 34. The app does not crash, the lists just don't populate and assumedly, does not save.
heres the code:
Save Data:
private fun saveData()
{
val sharedPref = getSharedPreferences(prefName,MODE_PRIVATE)
val gson = Gson()
val prefEditor = sharedPref.edit()
//prefEditor.clear()
prefEditor.putString(taskNameString, gson.toJson(gameifyInstance.GetTaskList()))
prefEditor.putString(taskDescString, gson.toJson(gameifyInstance.GetTaskDescList()))
prefEditor.putString(taskCostString, gson.toJson(gameifyInstance.GetTaskCostList()))
prefEditor.putString(rewardNameString, gson.toJson(gameifyInstance.GetRewardList()))
prefEditor.putString(rewardDescString, gson.toJson(gameifyInstance.GetRewardDescList()))
prefEditor.putString(rewardCostString, gson.toJson(gameifyInstance.GetRewardCostList()))
prefEditor.putString(pointTotalString, gameifyInstance.GetTotalPoints().toString())
prefEditor.commit()
}
Load data:
val sharedPref = getSharedPreferences(prefName,Context.MODE_PRIVATE)
val gson = Gson()
val rawTaskNameList = sharedPref.getString(taskNameString, "[]")
val rawTaskDescList = sharedPref.getString(taskDescString, "[]")
val rawTaskCostList = sharedPref.getString(taskCostString, "[]")
val rawRewardNameList = sharedPref.getString(rewardNameString, "[]")
val rawRewardDescList = sharedPref.getString(rewardDescString, "[]")
val rawRewardCostList = sharedPref.getString(rewardCostString, "[]")
val rawPointTotal = sharedPref.getString(pointTotalString, "0")
val type = object : TypeToken<List<String>>(){}
val loadedTaskNameList : List<String> = gson.fromJson(rawTaskNameList, type)
val loadedTaskDescList : List<String> = gson.fromJson(rawTaskDescList, type)
val loadedTaskCostList : List<String> = gson.fromJson(rawTaskCostList, type)
val loadedRewardNameList : List<String> = gson.fromJson(rawRewardNameList, type)
val loadedRewardDescList : List<String> = gson.fromJson(rawRewardDescList, type)
val loadedRewardCostList : List<String> = gson.fromJson(rawRewardCostList, type)
I've tried to fiddle with permissions, I've tried switching apply() and commit() when shaving shared preferences. The issue is I dont know where to debug etc to differentiate if this is a shared preferences issue or a permissions issue or any other issue.
EDIT: As I've been asked to post my getters, here they are: Getters for the singleton
they are just to return the string lists in my singleton that holds the tasks etc across the whole app
I have figured it out for those who may come across this
the culprit was in fact OnDestroy() where sharedpreferences saved just fine on the medium phone, it was not properly firing on the other phone. After moving around saveData() to other places and now something im looking to optimise more efficiently, the saving of shared preferences worked on other emulators besides medium phone 35
Thank you for your assistance