A simple solution would be to have a variable that dictates the current screen.
var showSecondScreen by remember { mutableStateOf(false) }
if (!showSecondScreen) {
Button(onClick = {showSecondScreen = true}){Text("Click to go next")}
} else {
Text("Second screen")
}
This doesn't have to be a boolean, you could declare var currentScreen by remember { mutableStateOf("homeScreen") }
and use a when
block for which screen to show.
@Composable fun MyApp(currentScreen: String) {
when (currentScreen) {
"homeScreen" -> HomeScreen()
"secondScreen" -> SecondScreen()
}
}
So you can think of it less as a transaction and more as a stateful navigation.
But you'll soon realize that this doesn't handle back navigation, so you'll need a navigation library like jetpack compose navigation, decompose, compose-router, etc. Here's more information on jetpack compose navigation.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…