The Navigation Component manages Fragment transactions, back-stack handling, and argument passing through a visual graph (nav_graph.xml) instead of manual FragmentTransaction calls.
<!-- res/navigation/nav_graph.xml (simplified) -->
<fragment android:id="@+id/listFragment" android:name=".ListFragment">
<action android:id="@+id/toDetail" app:destination="@id/detailFragment" />
</fragment>
<fragment android:id="@+id/detailFragment" android:name=".DetailFragment">
<argument android:name="itemId" app:argType="integer" />
</fragment>
With the Safe Args Gradle plugin enabled, navigating with arguments is fully type-checked at compile time — no more stringly-typed Bundle keys:
// From ListFragment
val action = ListFragmentDirections.toDetail(itemId = 42)
findNavController().navigate(action)
// In DetailFragment
private val args: DetailFragmentArgs by navArgs()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
loadItem(args.itemId)
}
If itemId‘s type or name ever changes in the nav graph, this code fails to compile rather than crashing at runtime — a significant improvement over passing raw Intent extras.
- Image
- SKU
- Rating
- Price
- Stock
- Availability
- Add to cart
- Description
- Content
- Weight
- Dimensions
- Additional information