Is jetpack compose navigation the only way to change screen in compose? do exist other ways to achieve it? After a research I only can find the codelab of navigation for switching between scrreens. Can't find any other official documented way.
s
Stylianos Gakis
07/18/2024, 4:30 PM
Technically the simplest way to do this is something as simple as this:
Copy code
enum class Screen {
One, Two, Three
}
fun App() {
var screen by remember { mutableStateOf(Screen.One) }
Surface(Modifier.fillMaxSize()) {
when(screen) {
One -> ScreenOne(navigateToTwo = { screen = Screen.Two })
Two -> ScreenTwo(navigateToThree = { screen = Screen.Three })
Three -> ScreenThree()
}
}
}
and as you change your state, a different screen will show. You don't need a navigation library just for this. But for a real project, some sort of navigation library is probably a good idea. androidx.navigation is just one of the options, but there are more.