I am working on an Android application using Kotlin and Jetpack Compose. To switch between the different screens I am using the NavHostController.
Initialization:
private lateinit var navController: NavHostController
setContent {
navController = rememberNavController()
Theme {
...
}
}
Usage:
NavHost(navController = navController, startDestination = *route*) {
composable(route = *route*) {
}
composable(route = *route*) {
}
...
}
In each composable
I call navController.navigate(route = *route*)
to switch between screens.
EDIT ( I call the navController.navigate()
inside a ChildComposable
, which is inside the composable
).
I use one Activity
only, which has no background set. This is how I add a background image to all the screens:
@Composable
fun ContentWithBackground(
content: @Composable () -> Unit
) = Box(modifier = Modifier.fillMaxSize()) {
Image(
painter = rememberAsyncImagePainter(model = R.drawable.background),
contentDescription = "Logo",
contentScale = ContentScale.Crop,
modifier = Modifier.fillMaxSize()
)
content()
}
The way I use the navigation causes flashing when the screens are changed and the new content is replacing the previous one. Basically, flickering. I guess this is somewhat of an expected behaviour.
However, is there a way to fix this? How can the change happen smoothly?
Here is a video of the flashing: