I am using `avedInstanceState`in dev14 but getting...
# compose
a
I am using `avedInstanceState`in dev14 but getting this error
cannot be saved using the current UiSavedInstanceStateRegistry. The default implementation only supports types which can be stored inside the Bundle. Please consider implementing a custom Saver for this class and pass it to savedInstanceState() or rememberSavedInstanceState()
z
What type are you trying to save?
a
Custom object
z
That error message is pretty clear imo, what part doesn’t make sense?
a
Do I need to add
saver
in custom object part ?
f
dev14 or dev17?
a
dev14
f
try dev17
a
okay
z
You pass the saver to
rememberSavedInstanceState
a
Copy code
val navigationSaver = Saver<NavigationStack<MainScreen>, NavigationStack<MainScreen>>(
    save = { it },
    restore = {  NavigationStack(init = MainScreen.Watchlist) }
)

this.navigation = rememberSavedInstanceState<NavigationStack<MainScreen>>(saver = navigationSaver) {
    NavigationStack(init = MainScreen.Watchlist)
}
Above code is giving me
java.lang.IllegalStateException: Check  failed.
error
Check failed.
at androidx.ui.savedinstancestate.ValueProvider$updateAndReturnValue$1.invoke(RememberSavedInstanceState.kt:106)
at androidx.ui.savedinstancestate.UiSavedStateRegistryImpl.performSave(UiSavedStateRegistry.kt:116)
at androidx.ui.core.SavedStateDelegate.dispatchSaveInstanceState(SavedStateDelegate.kt:106)
`at androidx.ui.core.AndroidComposeView.dispatchSaveInstanceState(AndroidComposeView.kt:690)``
z
Is
NavigationStack
Serializable
or
Parcelable
? The error message you posted initially says “The default implementation only supports types which can be stored inside the Bundle”
a
It is niether. It just a simple class
I am trying do the above same way
z
Right, so you need to return something from the
save
lambda that can be stored in a
Bundle
.
a
Is there any example for this ?
z
The example you posted does it. It returns/restores
Holder.value
, which is an
Int
, i.e. something that can be stored in a
Bundle
.
👍 1
The saver is basically your serialization/deserialization logic, but instead of serializing to a byte array, you’re serializing to any of the types supported by
Bundle
.
a
Got your point thanks