I tried to share some states between my screens in...
# compose
a
I tried to share some states between my screens in NavHost but it doesn't update when the appState change !!! Any one know why it doesn't recompose when the appState change ?
s
It should work. Does
AppState
itself change, or some internal state of
AppState
? If it’s internal state, is that defined as a
MutableState
or a simple
var
? A peek inside your
AppState
would make it easier to debug.
a
It's internal State
s
Could you show the call-site as well? At a glace, the
bottombarVisibility
and
drawerVisibility
should not be `var`s, since they themselves should not be changing, but their internal state. But there doesn’t seem to be anything wrong with the
isLandscape
part. It relies on
windowSizeClass
which is a
val
and should only be updated when you construct a whole new
AppState
object. Can I see where you’re constructing and remembering your
AppState
object as well?
r
I think this is the same problem I had, which I fixed this way: https://kotlinlang.slack.com/archives/CJLTWPH7S/p1660238432465599
a
yes, It's the same problem as mine.... I didn't know how to implement your solution
r
This is the structure of what I did:
Copy code
@Composable
fun AppNavGraph(
    ...
    settings: Settings
) {
    val settingsAsState = rememberUpdatedState(settings)
    NavHost(...) {
        settingsNavGraph(
            ...
            settings = { settingsAsState.value }
        )
    }
    ...
}

fun NavGraphBuilder.settingsNavGraph(
    ...
    settings: () -> Settings
) {
    navigation(...) {
        composable(
            route = ...,
        ) {
            SettingsScreen(
                ...
                settings = settings()
            )
        }
        ...
    }
}
Except you do it for
appState
, not
settings
.
a
Thank you ❤️ ❤️