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.
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:
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/nav_graph"
app:startDestination="@id/mainFragment">
<fragment
android:id="@+id/mainFragment"
android:name="com.jd.demo.ui.main.MainFragment"
android:label="main_fragment"
tools:layout="@layout/main_fragment_2">
<action
android:id="@+id/action_to_mainFragment2"
app:destination="@+id/mainFragment2"
app:enterAnim="@anim/slide_in_right"
app:exitAnim="@anim/slide_out_left"
app:popEnterAnim="@anim/slide_in_left"
app:popExitAnim="@animslide_out_right" />
</fragment>
<fragment
android:id="@+id/mainFragment2"
android:name="com.jd.demo.ui.main.MainFragment2"
android:label="main_fragment_2"
tools:layout="@layout/main_fragment_2" />
</navigation>In the activity layout, host the navigation graph with NavHostFragment:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<fragment
android:id="@+id/my_nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph" />
</LinearLayout>Navigate between fragments in Kotlin:
val view = inflater.inflate(R.layout.main_fragment_3, container, false)
val button1 = view.findViewById<Button>(R.id.button3)
button1.setOnClickListener { v ->
Navigation.findNavController(v).navigate(R.id.action_mainFragment2_to_mainFragment1)
}
return viewPass 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:
<deepLink app:uri="www.jd.com" />
<deepLink app:uri="www.jd.com.*" />
<deepLink app:uri="www.jd.com/{myarg}" android:autoVerify="true" />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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
