PreCompose icon indicating copy to clipboard operation
PreCompose copied to clipboard

value didn't update in NavHost

Open RYXCP opened this issue 2 years ago • 8 comments

In 1.5.10, based on the sample app called Reply, I gave it a try. However, I found that the screenType in the scene retains its initial value while the one outside the NavHost is changed correctly.

Should I not have done this?

//This is Reply
val contentType: ReplyContentType = ReplyContentType.SINGLE_PANE
...
@Composable
private fun ReplyNavHost(
    navController: NavHostController,
    contentType: ReplyContentType,
    ...
) {
    NavHost(
        navController = navController,
        startDestination = ReplyRoute.INBOX,
    ) {
        composable(ReplyRoute.INBOX) {
            ReplyInboxScreen(
                contentType = contentType,
                ...
//This is my app
var screenType = ScreenType.MEDIUM
...
@Composable
fun NavGraph(
    navController: Navigator,
    screenType: ScreenType,
){
    Text("${screenType}")
    NavHost(
        navigator = navController,
        initialRoute = Route.CHAT_ROUTE,
    ){
        scene(
            route = Route.SETTING_ROUTE
        ){
            Text("${screenType}")
        }

RYXCP avatar Feb 18 '24 19:02 RYXCP

Can you try 1.6.0-beta02 and check if it's working for you?

Tlaster avatar Feb 19 '24 01:02 Tlaster

oh, thanks! It does work

RYXCP avatar Feb 19 '24 08:02 RYXCP

I'm having a similar issue on 1.6.0. Any hint how to debug this?

martinbonnin avatar Apr 10 '24 21:04 martinbonnin

I'm having a similar issue on 1.6.0. Any hint how to debug this?

When i tried the "1.6.0-beta02", i found that it resolved this issue on its own.

RYXCP avatar Apr 11 '24 01:04 RYXCP

Thinking more about it, I think this might be the desired behaviour or else deeplinks might break. All the parameteres should come from the deeplink url. But not 100% sure yet

martinbonnin avatar Apr 11 '24 08:04 martinbonnin

I have a very similar issue in 1.6.0-rc05. The only difference in my case that I use 2 NavHost components and one is nested into other. And Screen in nested NavHost retains its initial value.

Here is example:

var screenType = ScreenType.MEDIUM ... @Composable fun NavGraph( navController: Navigator, screenType: ScreenType, ) { Text("${screenType}") NavHost( navigator = rememberNavigator(), initialRoute = Route.CHAT_ROUTE, ) { scene( route = Route.SETTING_ROUTE ) { NavHost( navigator = rememberNavigator(), initialRoute = Route.ENOTHER_ROUTE, ) { scene( route = Route.ENOTHER_ROUTE ) { Text("${screenType}") } } } } }

polis avatar Apr 26 '24 13:04 polis

If you find that the screen always displays the previous value which is outdated, maybe these will help:

In current version, updating the value outside of a NavHost could not trigger the recomposition( I guess) , namely passing values directly into a NavHost is not recommended.

It seems like you want to observe to the window size, perhaps setting up a kotlin object and check it in somewhere or simply delaying the update to NavHost scope, will be better.

RYXCP avatar Apr 26 '24 20:04 RYXCP

If you find that the screen always displays the previous value which is outdated, maybe these will help:

In current version, updating the value outside of a NavHost could not trigger the recomposition( I guess) , namely passing values directly into a NavHost is not recommended.

It seems like you want to observe to the window size, perhaps setting up a kotlin object and check it in somewhere or simply delaying the update to NavHost scope, will be better.

Thank you for suggestion! I moved to Navigation from JetBrains. It still in Alfa but it does not have this problem and my project is not commercial yet.

polis avatar May 21 '24 19:05 polis