Skip to content

Instantly share code, notes, and snippets.

@KaustubhPatange
Created June 4, 2022 10:02
Show Gist options
  • Save KaustubhPatange/80d5d3289dd1695754d0ea7dd261c573 to your computer and use it in GitHub Desktop.
Save KaustubhPatange/80d5d3289dd1695754d0ea7dd261c573 to your computer and use it in GitHub Desktop.
// 1. Create a destination
sealed class Destination : Parcelable { // parcelable so that it can be saved in a bundle.
object First : Destination()
data class Second(val name: String, val hobbies: List<String>) : Destination()
}
// 2. Setup the navigation
@Composable
fun MainScreen() {
// Create a NavController
val controller = rememberNavController<Destination>(
startDestination = Destination.First
)
NavBackHandler(controller) // Very important, otherwise system back action will not pop the backstack.
// Could be NavHost or etc.
AnimatedNavHost(
controller = controller,
transitionSpec = ...
) { destination ->
when(destination) {
Destination.First -> { /* composable content */ }
Destination.Second -> { /* composable content */ }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment