Navigation refers to the interactions that let users navigate across, into, and back out from the different pieces of content within your app.
The following table provides an overview of the three key concepts in navigation and the main types that you use to implement them.
Navigation component is a collection of tooling, libraries & plugins.
To implement bottom navigation is complex so navigation library helps to do seamlessly. because it manages the backstack, deep linking and automate the fragment transaction
importantly it collect all these information and present at one UI editor which is easy to use. called navigation graph
you can arrange activity, fragment and custom view using navigation graph a visual representation
3 main component of navigation library
is a new resource type. its an editor to visualise the navigation between activity and fragments as a source and destination.
The arrows between screens are action on click, it will show the ui of back stack, animation and argument pass information
this is all part of the XML of graph
Navigation Graph
It is basically a window which swaps in and out the fragments in ithat included in the navigation graph
written in the code and instruct the navigation to occur
we specify the action to be followed
It is better way to do the same
Generates the classes based on the navigation graph
The coding changed like this if you use safe args plugin
By generated args class to pass an argument is easy
for these component navigation UI provide libraries for ex by adding navigation library to the bottom navigation code you can get all these features by default. by that single line of setupwithnavcontroller.
The navigation controller is one of the key concepts in navigation. It holds the navigation graph and exposes methods that allow your app to move between the destinations in the graph.
When using the Navigation component, you create a navigation controller using the NavController class. NavController is the central navigation API. It tracks which destinations the user has visited, and allows the user to move between destinations. This guide demonstrates how to create a NavController in your app.
Typically, you first get a NavHostFragment, and then retrieve the NavController from the fragment. The following snippet demonstrates this:
val navHostFragment =
supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
val navController = navHostFragment.navController
The Navigation component uses a navigation graph to manage your app's navigation. The navigation graph is a data structure that contains each destination within your app and the connections between them.
As the snippet demonstrates, instead of passing the NavController to your composables, expose an event to the NavHost. That is, your composables should have a parameter of type () -> Unit for which the NavHost passes a lambda that calls NavController.navigate().