PreCompose icon indicating copy to clipboard operation
PreCompose copied to clipboard

scene invoked twice when used the variable navigation

Open N7ghtm4r3 opened this issue 1 year ago • 2 comments

Describe the bug Hi, when I use the variable navigation the invocation of the current scene is invoked twice and this cause a double recomposition of that scene

To Reproduce Steps to reproduce the behavior:

  1. Create a Compose Multiplatform project
  2. Edit the App() method with the snippet here below
  3. See error in the console where will be printed Hi two times

Expected behavior Single invocation of the scene, so Hi printed only once

Minimal reproducible example

PreComposeApp {
    val navigator = rememberNavigator()
    NavHost(
        navigator = navigator,
        initialRoute = "initial_route"
    ) {
        scene(
            route = initial_route
        ) {
            // content
        }
        scene(
            route = "any_route_path/{$any_id}"
        ) { backstackEntry ->
            // using 
           backstackEntry.pathMap[any_id]
           // for example
           println("Hi")
           // the content of this scene will be invoked twice and related its recomposition
        }
    }
}

N7ghtm4r3 avatar Jun 22 '24 15:06 N7ghtm4r3

Duplicate with #162 , but the fix in that PR is not working for current version, I still try to find a way to avoid unnecessary recomposition.

Tlaster avatar Jun 22 '24 16:06 Tlaster

Thank you for the info!

N7ghtm4r3 avatar Jun 22 '24 16:06 N7ghtm4r3

Hi, any updates about this issue?

The current workaround is invoking, for example a fetch request to update the UI data, after 3 times:

  • NavHost where the ProjectsScreen is invoked
PreComposeApp {
    navigator = rememberNavigator()
    Theme {
        NavHost(
            navigator = navigator,
            initialRoute = PROJECTS_SCREEN
        ) {
            scene(
                route = PROJECTS_SCREEN
            ) {
                ProjectsScreen().ShowContent()
            }
        }
    }
 }
  • ProjectsScreen where there is the workaround:

  private val viewModel = ProjectsScreenViewModel(
      snackbarHostState = snackbarHostState
  )

  companion object {

      var int = 0

  }

  init {
      if(int == 3) { // this currently seems to works as workaround (on Desktop this value is 4) 
          viewModel.setActiveContext(this::class.java)
          viewModel.getProjects()
      } else
          int++
  }

N7ghtm4r3 avatar Sep 24 '24 13:09 N7ghtm4r3

No update yet, since compose multiplatform 1.7 is around the corner, I need to migrate to the compose 1.7 first, which might not be an easy work since SeekableTransitionState API has changed since 1.7.

Tlaster avatar Sep 25 '24 02:09 Tlaster

This behavior not happens in the 1.7.0-alpha03 version

N7ghtm4r3 avatar Oct 21 '24 09:10 N7ghtm4r3