Mobile Development 11 min read

Android Navigation Component: Introduction, Usage, and Source Code Analysis

This article provides a comprehensive guide to Android's Navigation component, covering its purpose, setup steps, XML graph configuration, fragment navigation, data passing methods, deep link handling, nested navigation, and an analysis of the underlying source code with practical Kotlin examples.

JD Retail Technology
JD Retail Technology
JD Retail Technology
Android Navigation Component: Introduction, Usage, and Source Code Analysis

Google I/O 2018 introduced Android Jetpack, which includes the Navigation component to simplify fragment stack management, enable deep links, and provide type‑safe argument passing.

Why use Navigation? It eliminates IllegalStateException errors, supports direct fragment navigation via deeplinks, and offers safer parameter transmission.

Setup – Add the following Gradle dependencies (replace the version as needed):

dependencies {
    nav_version = '1.0.0-alpha01'
    implementation "android.arch.navigation:navigation-fragment:$nav_version"
    implementation "android.arch.navigation:navigation-ui:$nav_version"
}

Define a navigation graph (nav_graph.xml) that declares fragments and actions:

In the activity layout, host the navigation graph with NavHostFragment :

Navigate between fragments in Kotlin:

val view = inflater.inflate(R.layout.main_fragment_3, container, false)
val button1 = view.findViewById
(R.id.button3)
button1.setOnClickListener { v ->
    Navigation.findNavController(v).navigate(R.id.action_mainFragment2_to_mainFragment1)
}
return view

Pass data using a Bundle :

Bundle bundle = new Bundle()
bundle.putString("amount", amount)
Navigation.findNavController(view).navigate(R.id.confirmationAction, bundle)

Or use the Safe Args Gradle plugin for type‑safe arguments:

val action = SpecifyAmountFragmentDirections.confirmationAction()
action.setAmount(amount)
Navigation.findNavController(view).navigate(action)

Configure deep links in the navigation XML:

Override onSupportNavigateUp in the host activity to handle back navigation correctly:

@Override
public boolean onSupportNavigateUp() {
    return Navigation.findNavController(this, R.id.my_nav_host_fragment).navigateUp();
}

Source‑code analysis – NavHostFragment creates a NavController in onCreate and a FrameLayout in onCreateView to host destinations. It registers the controller with the view hierarchy so that Navigation.findNavController can locate it.

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Context context = getContext();
    mNavController = new NavController(context);
    mNavController.getNavigatorProvider().addNavigator(createFragmentNavigator());
}

@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    FrameLayout frameLayout = new FrameLayout(inflater.getContext());
    frameLayout.setId(getId());
    return frameLayout;
}

The NavController parses the navigation XML, builds a graph of NavDestination objects, and delegates actual navigation actions to concrete implementations such as FragmentNavigator and ActivityNavigator . The article concludes that Navigation offers powerful, safe, and visual navigation capabilities, including deep links, and encourages developers to adopt Kotlin and KTX extensions.

AndroidKotlinFragmentnavigationDeepLinkJetpack
JD Retail Technology
Written by

JD Retail Technology

Official platform of JD Retail Technology, delivering insightful R&D news and a deep look into the lives and work of technologists.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.