scene invoked twice when used the variable navigation
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:
- Create a Compose Multiplatform project
- Edit the
App()method with the snippet here below - See error in the console where will be printed
Hitwo 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
}
}
}
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.
Thank you for the info!
Hi, any updates about this issue?
The current workaround is invoking, for example a fetch request to update the UI data, after 3 times:
-
NavHostwhere theProjectsScreenis invoked
PreComposeApp {
navigator = rememberNavigator()
Theme {
NavHost(
navigator = navigator,
initialRoute = PROJECTS_SCREEN
) {
scene(
route = PROJECTS_SCREEN
) {
ProjectsScreen().ShowContent()
}
}
}
}
-
ProjectsScreenwhere 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++
}
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.
This behavior not happens in the 1.7.0-alpha03 version