The purpose of the Navigation Architecture Component is to introduce a standardized approach to managing navigation in Android apps. It also significantly reduces the amount of code that's required for common navigation tasks.
- [Instructor] In May of 2017, Google introduced a set of libraries known as the Architecture Components. These included the ViewModel component, which was designed to separate concerns between the user interface, what the user sees, and the business logic, the LiveData component that implements a publish/subscribe reactive programming model, and the Room library, which simplifies the creation and management of your local SQLite databases on Android devices.
These components and many others are now grouped together under a label known as Android Jetpack, a set of tools that can make it easier to build sturdy, robust applications for Android devices. The Navigation Component is one of these newer tools. Its goals are to help you implement the best practices for navigation between screens in an Android app. It lets you separate the definition of what it calls destinations from your business logic.
So, the definition, say, of an activity or a fragment is given a resource ID that you can reference in your programming instead of the names of the classes themselves. This makes your apps easier to maintain over time. The most common practice when using the Navigation Component is to architect your application as a single activity and then to create each screen as a fragment. You then use the Navigation Component to move those fragments in and out of the activity.
To the user, it looks exactly the same as before, when you might have used multiple activities, but everything is kept in a single, stateful environment. The Navigation Component makes a lot of common tasks a lot easier than they were before. For example, it's much simpler than previously to pass data between screens using a combination of the tools build in to the Navigation Component plus another component known as safe-args. And it also simplifies some very common navigation tasks, such as setting up deep links that let you link to a screen in your app and animating of transitions between screens.
Prior to the Navigation Component, navigation was accomplished using intents. You would create an instance of an intent, you'd pass in the current context and the class of an activity you wanted to navigate to, and then you'd start that activity. If you wanted to pass data, that would be done using extras. And the code could get pretty verbose. That same code would look like this in Kotlin. Once again, you'd create an instance of the intent class and then pass that intent object to the start activity function.
When you move to the Navigation Component, you end up with a lot less code. Each screen, again, is a fragment that's hosted by a single activity and each of those fragments is defined as a destination in a file called a navigation graph. This is an xml resource file that I'll show you how to create. You then place a fragment in the activity. It's called the navigation host and it's an instance of a specific class that's a part of the Navigation Component.
And then, in order to navigate to the fragment, you call a function called finNavController, you pass in the resource ID of the navigation host fragment, and from there, you call the navigate function and pass in the resource ID of the destination. As a result, you're no longer having to remember while coding what the exact name of a fragment or a class is. You're simply referring to resource ID's. You end up with a lot less code and the Navigation Component handles all the runtime behavior for you.
Here's another example of how the Navigation Component reduces the amount of code you need. If you wanted to create a deep link in the past, you would have had to create an intent filter in your application manifest. It would've required a name, probably a couple of categories, a couple of schemes if you wanted to support both http and https, then a host, and a pattern. And all that code together would register that activity as a potential receiver of an intent.
When you move to the Navigation Component, this code is reduced to a single element in the navigation graph. It's called a deep link element and you pass in a URI and you indicate where you want parameters to be placed in the URI and what they'll be known as when they're received. That's all you need. In the following videos, I'll show you how to get started with the Navigation Component, how to register the required dependencies in your gradle files, and then, how to build and use your navigation graph, and how to create and use deep links.