I would like to store a Parcelable type via `remem...
# compose
l
I would like to store a Parcelable type via
rememberSaveable
but it doesn't get restored, instead it jumps always into the init method of
rememberSaveable
. What am I doing wrong?
Copy code
val navigator: Navigator<T> by rememberSaveable(
        stateSaver = Navigator.saver(backDispatcher = backDispatcher),
        key = "State
    ) {
        println("initial")

        mutableStateOf(
            Navigator(
                initialConfiguration = initialConfiguration,
                backDispatcher = backDispatcher
            )
        )
    }

        fun <T : Parcelable> saver(
            backDispatcher: OnBackPressedDispatcher
        ) = listSaver<Navigator<T>, T>(
                save = { navigator -> navigator.backStack.toList() },
                restore = { backStack -> Navigator(backStack, backDispatcher) }
            )
I noticed that
rememberSaveable
is called internally multiple times, first time with my provided key and the other times it jumps into the else branch and generates a hash. Is this a bug? rememberSaveable:
Copy code
val finalKey = if (!key.isNullOrEmpty()) {
        key
    } else {
        currentCompositeKeyHash().toString()
    }
a
yes, I expect it to work. will you be able to file a bug with a sample project which helps us to reproduce it? thanks
l
@Andrey Kulikov Sure. https://issuetracker.google.com/issues/180701630 Is there a workaround I can use meanwhile?
a
so, first of all you don’t really need mutableStateOf as you never update the variable with a new Navigator object:
Copy code
val navigator: Navigator<T> = rememberSaveable(
    saver = Navigator.saver(backDispatcher = backDispatcher),
    key = "State"
) {
    Navigator(
        initialConfiguration = initialConfiguration,
        backDispatcher = backDispatcher
    )
}
and looks like it works like this? but I will try to figure out why it didn’t work with state on Monday
l
Ah cool, it works now. I have followed the instructions here: https://developer.android.com/jetpack/androidx/releases/compose-foundation#1.0.0-alpha12 Unfortunately the docs haven't been updated yet. I have also filed a bug for this. See https://kotlinlang.slack.com/archives/CJLTWPH7S/p1613674963285400