Contents
CWCoding With DhrumilCW Practical Flutter and Kotlin tutorials for working mobile developers

Home › Articles

Getting Started With Jetpack Navigation in Kotlin

By Rahul Mehta · Published · 3 min read

Compass and map with arrows representing app navigation component setup.
Compass and map with arrows representing app navigation component setup. · Foto: MasterTux / Pixabay

Jetpack Navigation is the standard way to move between screens in a Kotlin Android app. It replaces manual FragmentTransactions with a single navigation graph that describes destinations, actions, and arguments, so your back stack behaviour stays predictable as the app grows.

This guide walks through a practical first setup: adding dependencies, building a nav graph, wiring NavHost, passing arguments, and handling the edge cases that trip up most beginners.

Add the Navigation Dependencies

Start by adding the navigation-fragment-ktx and navigation-ui-ktx artifacts to your app module. In most projects you will also want the Safe Args Gradle plugin, which generates type-safe directions classes from your graph. Keep the Navigation version aligned with your compileSdk; mismatches show up as obscure class-not-found crashes at runtime.

If you use View Binding, enable it in the same module so each destination can inflate its layout without findViewById. For Compose screens, use navigation-compose instead of the fragment artifacts. The rest of this guide assumes Fragments, which is still the most common setup in existing codebases.

  • Add navigation-fragment-ktx and navigation-ui-ktx
  • Apply the safeargs plugin for type-safe arguments
  • Enable View Binding to avoid findViewById
  • Keep Navigation version aligned with compileSdk

Create the Navigation Graph

Create a res/navigation folder and add nav_graph.xml. Each destination is a fragment element with an id and a layout reference. Actions connect one destination to another, and you can set enterAnim, exitAnim, popEnterAnim, and popExitAnim directly on the action to keep transitions consistent.

Put the NavHostFragment in your main activity layout and point app:navGraph to the graph file. Set app:defaultNavHost to true so the system back button is handled by Navigation. When you run the app, the start destination appears automatically; you never call a transaction yourself.

Inside a fragment, call findNavController().navigate(R.id.action_home_to_detail). With Safe Args you instead call navigate(HomeFragmentDirections.actionHomeToDetail(itemId)), and the generated class enforces the argument type at compile time. That single change removes a whole class of runtime crashes from stringly-typed bundles.

For simple values, declare arguments in the graph with argType. For larger objects, pass an ID and load the data from your repository or database. Bundles have a practical size limit around 500KB on many devices, so passing full models between screens is a shortcut that will eventually fail in production.

  • Use Safe Args directions for compile-time checked arguments
  • Declare arguments in the graph with argType
  • Pass IDs, not large objects, between destinations
  • Keep bundle payloads small

Handle the Back Stack and Up Button

Navigation manages the back stack for you, but you still need to think about pop behaviour. Use popUpTo with inclusive flags when you want to clear intermediate screens, for example after a login flow. Without it, pressing back returns users to the login screen, which is almost never what you want.

Configure the toolbar with setupWithNavController so the up arrow and title update per destination. If your app uses a DrawerLayout or BottomNavigationView, NavigationUI can wire those too. If you are comparing this approach with manual FragmentTransactions, the Jetpack Navigation vs Manual Fragment Transactions article breaks down the trade-offs.

6Manual transaction2.5Navigation + Safe 2Compose Navigation9Custom router
Typical time to wire up navigation for a 6-screen flow (hours)

Deep links let an external URL open a specific destination. Declare a deepLink element inside the destination with a URI pattern, then handle the incoming intent through NavController. For notifications, build a PendingIntent with the same URI so tapping it lands on the right screen with the right arguments.

Global actions are useful for destinations reachable from many places, such as a settings screen. Use them sparingly: a graph full of global actions is hard to read, and it usually means the screen should be a separate activity or a dialog destination instead.

Navigation approaches compared for a small Kotlin app
ApproachSetup effortBack stack safetyType-safe argsBest for
Manual FragmentTransactionsLowManual, error-proneNoTiny apps, legacy code
Jetpack Navigation (Fragments)MediumBuilt inYes, with Safe ArgsMost new Fragment apps
Jetpack Navigation (Compose)MediumBuilt inYes, via route typesCompose-first apps
Single Activity + custom routerHighCustomPartialLarge teams with strict patterns

Common Beginner Mistakes

The most frequent problem is calling navigate twice in quick succession, which pushes duplicate destinations. Guard clicks with a debounce or check the current destination before navigating. Another classic is holding a NavController reference in a ViewModel; keep navigation decisions in the UI layer and expose events instead.

Test your flows on the system back button, not just the in-app one. Also verify argument defaults when a destination is opened via deep link, because those entry points skip the action that normally supplies values.

  • Debounce rapid navigate calls
  • Never store NavController in a ViewModel
  • Test system back behaviour, not just toolbar back
  • Check argument defaults for deep-link entry
  • Use a single nav_graph.xml and NavHostFragment instead of manual transactions
  • Enable Safe Args early; it prevents argument crashes at compile time
  • Pass IDs between screens, not large objects, to keep bundles small
  • Test system back and deep-link entry points before shipping
Rahul Mehta · Mobile tooling writer
Specialises in setup guides, build tooling and performance checks for mobile projects. Regularly benchmarks lists, scrolling and startup behaviour to give readers concrete numbers.

Related articles

↑