Is there a way to save map/mutableMap into savedInstanceState?
other than using ViewModel functions. I'm curious to know his point...
thanks in advance
I worked on it. wonder why i didn't find anyone asking about this before, probably there are better ways. but in case that someone needs to know how this is done. i did it this way...
i used onSaveInstanceState to check if the map was initialized, then proceeded
override fun onSaveInstanceState(savedInstanceState: Bundle) {
super.onSaveInstanceState(savedInstanceState)
if (::answersMap.isInitialized){
val keys = (answersMap.keys).toIntArray()
val values = (answersMap.values).toBooleanArray()
savedInstanceState.putIntArray(KEYS, keys)
savedInstanceState.putBooleanArray(VALUE, values)
}
}
also on the onCreate function I retrieved the arrays and created the mutablemap i needed
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val keysArray = savedInstanceState?.getIntArray(KEYS) ?: IntArray(0)
val valuesArray = savedInstanceState?.getBooleanArray(VALUE) ?: BooleanArray(0)
answersMap = mutableMapOf<Int, Boolean>().apply {
for (i in keysArray.indices) this [keysArray[i]] = valuesArray[i]
}
in case you were wondering about the answerMap type
private lateinit var answersMap : MutableMap<Int, Boolean>
IT WORKED!!!