Android Navigation Actions: Defined Paths, Not Just Destinations

A Navigation Action is a configured pathway between screens, not just a destination. It bundles the target screen with transition animations and arguments. Use them in your XML nav graph to keep UI code clean.
WHY IT EXISTS Navigation Actions exist to centralize navigation logic. Without them, your UI code (like a button's click listener) would need to know which screen to go to, what animations to use, and what data to pass. Actions move this configuration into a single source of truth: the navigation graph XML file.
THE MENTAL MODEL Think of a Navigation Action as a pre-configured hyperlink on a webpage. A basic link just takes you to a URL (the destination). A Navigation Action is a smarter link: it knows the destination, but it also defines the transition animation, what screens to remove from the back stack, and what arguments to pass. You trigger the whole configured event with one ID.
HOW IT WORKS In your res/navigation/nav_graph.xml file, you define an <action> inside a destination's <fragment> tag. This action has its own ID and points to a app:destination. You can add attributes like app:enterAnim for animations or app:popUpTo to control the back stack. In your Kotlin code, you navigate by calling navController.navigate(R.id.your_action_id), not the destination's ID. This ensures the entire configured path is executed.
WHEN TO USE IT Use actions for all statically defined navigation paths in Android's Navigation Component. They make your navigation graph a visual and functional source of truth, making your app's flow easier to understand and maintain. They also enable type-safe argument passing via the Safe Args plugin.
WHEN NOT TO USE IT Avoid using actions when the navigation is completely dynamic and the destination cannot be known at compile time. In such rare cases, you might navigate directly by destination ID. However, for over 95% of screen-to-screen navigation, actions are the correct and intended approach.
ONE CANONICAL EXAMPLE A user on a LoginFragment taps a 'Sign In' button. This triggers action_loginFragment_to_homeFragment. This action is configured in the XML to navigate to HomeFragment and also includes app:popUpTo="@id/loginFragment" and app:popUpToInclusive="true". This ensures that after a successful login, the user cannot press the back button to return to the login screen.
Read the original → developer.android.com
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.