Hi ! Looking at <https://developer.android.com/jet...
# compose
l
Hi ! Looking at https://developer.android.com/jetpack/compose/state, it is written that we may need to create custom object saver to pass custom data to
rememberSaveable
, but how should I build such object ? Let's say for
data class Point(val x: Int, val y:Int)
? Because there is not any example in the documentation, and I did not understand this sentence (
build a custom object saver
), what interface should the saver have ? What is the Android class/object responsible for such interface ? Should I create a
Bundle
myself ?
l
Thank you @Jorkoh. I've also managed by using a simple String serialization
Here what I've achieved
j
I think the
mapSaver
sample would work nicely for this case but doing it with a String also works 👍
👍 1
a
You should use
rememberSaveable(stateSaver = saver) { mutableStateOf(state) }
and remove the
MutableState
type from your saver.
👍 1
l
Thank you @Albert Chang. I've tried to remove
mitableStateOf
from the saver, but this time I get an error
Type 'DndData' has no method 'getValue(Nothing?, KProperty<*>)' and thus it cannot serve as a delegate
.
a
You shouldn't remove the
mutableStateOf()
in
rememberSaveable
. If you didn't, check if you are using the correct overload of
rememberSaveable
.
👍 1
l
Thank you, in this attempt I get an error
inferred data is MutableState<DndData> but type DndData expected
.
a
You are using the wrong overload. You need to use the version with
stateSaver
parameter.
👍 1
l
Thank you, it worked. And I can use delegation with
by
again.