Сообщество StackOverflow!
У меня возникла проблема с навигацией Jetpack Compose с использованием NavHost и RememberNavController. Большую часть времени мое приложение работает должным образом, но иногда, когда я нажимаю кнопки для переключения между экранами, содержимое исчезает, и видимой остается только поверхность.


Вот упрощенная версия моего кода:
Код: Выделить всё
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
val navController = rememberNavController()
NavigationTestTheme {
Surface(
modifier = Modifier.fillMaxSize(),
color = Color.Cyan
) {
NavHost(navController = navController, startDestination = "First") {
composable(route = "First") {
FirstScreen(onButtonTap = { navController.navigate("Second") })
}
composable(route = "Second") {
SecondScreen(onButtonTap = { navController.popBackStack() })
}
}
}
}
}
}
}
@Composable
fun FirstScreen(onButtonTap: () -> Unit) {
Box(
contentAlignment = Alignment.Center,
modifier = Modifier
.fillMaxSize()
.background(Color.Blue)
) {
CustomButton(title = "To Second View") {
onButtonTap()
}
}
}
@Composable
fun SecondScreen(onButtonTap: () -> Unit) {
Box(
contentAlignment = Alignment.Center,
modifier = Modifier
.fillMaxSize()
.background(Color.Green)
) {
CustomButton(title = "Pop View") {
onButtonTap()
}
}
}
@Composable
fun CustomButton(title: String, onClick: () -> Unit) {
Button(
onClick = { onClick() },
modifier = Modifier.size(width = 200.dp, height = 40.dp)
) {
Text(text = title)
}
}
- Display the FirstScreen on the initial launch.
- Navigate to the SecondScreen and add it to the back stack when the button in the FirstScreen is tapped.
- Pop the SecondScreen when the button in the SecondScreen is tapped, and display the FirstScreen again.

I've tried simplifying the code and isolating the issue, but I can't seem to pinpoint the root cause. Any suggestions or insights into what might be causing this issue would be greatly appreciated.
Thank you in advance for your help!
Источник: https://stackoverflow.com/questions/781 ... stinations