Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scene invoked twice when used the variable navigation #338

Open
N7ghtm4r3 opened this issue Jun 22, 2024 · 4 comments
Open

scene invoked twice when used the variable navigation #338

N7ghtm4r3 opened this issue Jun 22, 2024 · 4 comments

Comments

@N7ghtm4r3
Copy link

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
        }
    }
}
@Tlaster
Copy link
Owner

Tlaster commented Jun 22, 2024

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.

@N7ghtm4r3
Copy link
Author

Thank you for the info!

@N7ghtm4r3
Copy link
Author

N7ghtm4r3 commented Sep 24, 2024

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++
  }

@Tlaster
Copy link
Owner

Tlaster commented Sep 25, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants