-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Null Safety
-
Section 4: Object-Oriented Kotlin
-
Section 5: Functional Kotlin
-
Section 6: Coroutines
-
Section 7: Collections Deep Dive
-
Section 8: Type System Deep Dive
-
Section 9: Interop and Tooling
-
Section 10: Kotlin DSLs and Patterns
-
Section 11: Testing and Quality
-
Section 12: Server-Side Kotlin
-
Section 13: Practical Projects
-
Section 14: Interview Practice
-
Section 15: More Practice Exercises
-
Section 16: More Standard Library
-
Section 17: Multiplatform Kotlin
-
Section 18: kotlin.collections In Depth
-
Section 19: kotlin.text In Depth
-
Section 20: kotlin.ranges and kotlin.sequences
-
Section 21: kotlin.io and File Handling
-
Section 22: kotlinx.coroutines Deep Dive
-
Section 23: kotlin.reflect
-
Section 24: Android Development with Kotlin Overview
-
Section 25: Kotlin Multiplatform Deep Dive
-
Section 26: Kotlin for Backend Deep Dive
-
Section 27: Kotlin Design Patterns
-
Section 28: Advanced Language Features
-
Section 29: More Practice Exercises
-
Section 30: More Interview Practice
-
Section 31: Kotlin Type System Deep Dive
-
Section 32: Kotlin Null Safety Advanced
-
Section 33: Kotlin Testing Deep Dive
-
Section 34: Kotlin Build Tooling Deep Dive
-
Section 35: Kotlin Serialization
-
Section 36: Kotlin Performance Considerations
-
Section 37: Kotlin Native Overview
-
Section 38: Kotlin for Data and Scripting
-
Section 39: More Coroutines Practice
-
Section 40: More Android-Adjacent Patterns
-
Section 41: More Practical Projects
-
Section 42: More Design and Architecture Practice
-
Section 43: Kotlin Language Evolution
-
Section 44: More Interview and Review
-
Section 45: Kotlin Delegation Patterns Deep Dive
-
Section 46: Kotlin Annotations Deep Dive
-
Section 47: Kotlin for Gradle Plugin Development
-
Section 48: Kotlin Concurrency Beyond Coroutines
-
Section 49: Kotlin Compiler Internals
-
Section 50: Real-World Kotlin Case Studies
-
Section 51: Final Practice Projects
-
Section 52: Kotlin for Server-Side Reactive Programming
-
Section 53: More Practice and Drills
-
Section 54: Kotlin Security Practices
196: Practice Exercise: Building a Type-Safe Navigation Graph
I've seen this happen a dozen times: a developer switches to type-safe navigation and thinks, "Great, now I just use a data class instead of a String, and I've solved my routing problems." They treat the route object like a simple container, but they still try to manually parse arguments or use navArgument blocks in their destination definitions. They think the "type-safety" is just a fancy way of avoiding typos in a URL string.
Here is why that's wrong. If you're still manually defining arguments in your graph, you aren't actually using type-safe navigation; you're just using a wrapper. The whole point of the modern Kotlin Navigation approach is to move the source of truth from the NavGraph definition to the Serializable class itself. When you do it the old way, you're still duplicating your schema in two places: the class you use to navigate and the composable definition where you declare the arguments. That's just more code to keep in sync.
Thinking Type-Safe Navigation is Just String Constants in Disguise
In the old days (which was basically last year), we did this:
val route = "details/{movieId}" navController.navigate("details/$movieId") // In the graph composable(route) { backStackEntry -> val movieId = backStackEntry.arguments?.getString("movieId") MovieDetailsScreen(movieId) }Even if you put
"details/{movieId}"into a constant, it's still a String. If you change the argument name in the graph but forget to change it in thenavigatecall, the app compiles perfectly and then crashes at runtime. I can't tell you how many hours I've wasted hunting down a misplaced slash or a misspelled argument key in a complex graph.Letting the Compiler Own the Route Schema
The correct way is to let Kotlin Serialization handle the mapping. You define a
@Serializableobject or class, and that is the route. There is no String involved in your business logic.Let's look at a real-world example: a movie app where you need to pass a movie ID and a user's preferred playback quality.
@Serializable data class MovieDetails(val movieId: String, val quality: String) // Navigating is now a type-safe function call navController.navigate(MovieDetails(movieId = "m123", quality = "4K")) // Defining the destination composable<MovieDetails> { backStackEntry -> val args = backStackEntry.toRoute<MovieDetails>() MovieDetailsScreen(args.movieId, args.quality) }Notice what happened here. I didn't define a path. I didn't specify that
movieIdis a String. ThetoRoute<T>()function uses the generic type to automatically extract the arguments from the navigation bundle. If I add auserIdto theMovieDetailsclass, the code will literally refuse to compile until I update every singlenavigatecall in the entire app. That is the power of moving the logic from runtime to compile-time.One quick tip: keep your route classes lean. Don't pass huge data objects or complex custom classes through the navigation graph. Stick to IDs and primitives. If you need a full User object, pass the
userIdand let the destination screen fetch the data from a repository or a shared ViewModel. It keeps your navigation state small and preventsTransactionTooLargeExceptioncrashes.
📋 Practical Task
Exercise: Implementing a Type-Safe User Profile and Settings Flow
You are building a User Management module. You need to implement a type-safe navigation flow between a UserProfile screen and a UserSettings screen.
Requirements:
- Create a
@Serializabledata class forUserProfilethat accepts auserId: Stringand anisAdmin: Boolean. - Create a
@Serializabledata class forUserSettingsthat accepts auserId: Stringand asection: String(e.g., "Privacy", "Notifications"). - In your
NavHost, define the twocomposabledestinations using these types. - Inside the
UserProfiledestination, implement a button that navigates to theUserSettingsscreen, passing theuserIdfrom the current route and the string "Privacy" as the section. - Ensure you use
toRoute<T>()to retrieve the arguments in both screens.
Note: Assume the necessary Navigation and Kotlin Serialization dependencies are already configured in the project.
There are no comments for now.