`savedInstanceState` doesn't work in a NavHost. T...
# compose
m
savedInstanceState
doesn't work in a NavHost. This sample will always print '1'. Am I doing something wrong?
Copy code
NavHost(navController, startDestination = "search") {
    composable("search") {
        var counter by savedInstanceState { 0 }
        counter += 1
        Log.d("counter", counter.toString())
    }
}
😮 1
i
I suspect you're actually looking for
rememberSavedInstanceState
if you want the state to be remembered across compositions
m
@Ian Lake
savedInstanceState
should be
rememberSavedInstanceState { mutableStateOf(init()) }
. In fact my sample works if used in the Activity.setContent block
From compose source code
Copy code
@Composable
fun <T> savedInstanceState(
    vararg inputs: Any?,
    saver: Saver<T, out Any> = autoSaver(),
    key: String? = null,
    policy: SnapshotMutationPolicy<T> = structuralEqualityPolicy(),
    init: () -> T
): MutableState<T> = rememberSavedInstanceState(
    *inputs,
    saver = mutableStateSaver(saver, policy),
    key = key,
    init = { mutableStateOf(init(), policy) }
)
i
So
mutableStateOf
works, but
savedInstanceState
isn't working for you? Do you mind filing a bug with a sample project? https://issuetracker.google.com/issues/new?component=409828
h
Hey, even I’m facing a similar issue like this. I want to change the theme between light and dark on a click, for that I’m using
savedInstanceState
to store the current theme and change it. The issue is calling
setLightTheme(!isLightTheme)
will change the theme, but after that
isLightTheme
is not changed to false and the theme is not changed. Is this a bug? or am I doing something wrong?
Copy code
setContent {
    val (isLightTheme, setLightTheme) = savedInstanceState { true }
    MyProjectTheme(lightTheme = isLightTheme) {
        val navController = rememberNavController()
        NavHost(navController, startDestination = "home") {
            composable("home") {
                HomeScreen(
                    onClick = { setLightTheme(!isLightTheme) }
                )
            }
        }

    }
}
@manueldidonna were you able to solve this issue or find a workaround?
m
Nope, I haven't solved that issue. I'll file an issue in the tracker in the next hours
Done @Ian Lake
👍 2
h
Hey, is this issue fixed now in alpha-08 ?
Hi guys, wanted to know about this issue and the solution. I have gone through these issues • https://issuetracker.google.com/issues/174158395https://issuetracker.google.com/issues/173647680https://issuetracker.google.com/issues/172263305 The first and second are duplicates and last one is fixed, but still I’m facing this issue. I have tried on compose version 1.0.0-alpha10 and navigation version 1.0.0-alpha05 My use case: I want to toggle dark and light mode and I’m storing it as a state.
Copy code
setContent {

            val (isLightTheme, setLightTheme) = remember { mutableStateOf(true) }

            McComposeTheme(lightTheme = isLightTheme) {
                HomeScreen {
                    setLightTheme(!isLightTheme)
                }
            }
        }
But this doesn’t seem to work. Any help here please?
@Adam Powell