Thread
#compose
    l

    Lilly

    1 year ago
    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?
    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:
    val finalKey = if (!key.isNullOrEmpty()) {
            key
        } else {
            currentCompositeKeyHash().toString()
        }
    Andrey Kulikov

    Andrey Kulikov

    1 year ago
    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

    Lilly

    1 year ago
    @Andrey Kulikov Sure. https://issuetracker.google.com/issues/180701630 Is there a workaround I can use meanwhile?
    Andrey Kulikov

    Andrey Kulikov

    1 year ago
    so, first of all you don’t really need mutableStateOf as you never update the variable with a new Navigator object:
    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

    Lilly

    1 year ago
    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. Seehttps://kotlinlang.slack.com/archives/CJLTWPH7S/p1613674963285400