Navigation Component (Implementation in kotlin with real time example), fragmentfactory and dependency
What's implemented?
- single Activity with Fragments
- navigation graph in XML
- handling of notifications (explicit deep links)
- custom handling of implicit deep links (more on that later)
- passing arguments between Fragments via Safe Args
- starting Fragments for result and waiting for that result (similar to Activity's startActivityForResult and onActivityResult)
- transitions between fragments with shared Toolbar
- a simple Toolbar navigation icon morphing ("<-" -> "X")
- base Dagger setup with a FragmentFactory used to create Fragments with non-default constructors
To include Navigation support in your project, add the following dependencies to your app's build.gradle file:
def nav_version = "2.3.0-alpha05"
// Java language implementation
implementation "androidx.navigation:navigation-fragment:$nav_version"
implementation "androidx.navigation:navigation-ui:$nav_version"
// Kotlin
implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
// Dynamic Feature Module Support
implementation "androidx.navigation:navigation-dynamic-features-fragment:$nav_version"
// Testing Navigation
androidTestImplementation "androidx.navigation:navigation-testing:$nav_version"
}
implementation "androidx.navigation:navigation-ui:$nav_version"
// Kotlin
implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
// Dynamic Feature Module Support
implementation "androidx.navigation:navigation-dynamic-features-fragment:$nav_version"
// Testing Navigation
androidTestImplementation "androidx.navigation:navigation-testing:$nav_version"
}
You must also apply one of two available plugins.
To generate Java language code suitable for Java or mixed Java and Kotlin modules, add this line to your app or module's build.gradle file:
apply plugin: "androidx.navigation.safeargs"
apply plugin: "androidx.navigation.safeargs.kotlin"
We will be creating simple app to use navigation component to navigate though different fragment and pass value through argument.
Navigation library from Jetpack has recently reached RC1 and all Android developers should start considering it for new apps. I’m responsible for app architecture of Air Bank Germany — a new mobile-first German bank. Our app has a multi-module, single-activity architecture with ViewModels from Architecture Components. Integrating Navigation Component was a logical step, but it wasn’t without a few gotchas. In this blogpost, I want to share how we solved them. It’s a more advanced post, so I assume the readers’ knowledge of the official documentation.
We will be creating simple app to use navigation component to navigate though different fragment and pass value through argument.
Using Navigation Architecture Component in a large app
The Navigation component consists of three key parts that are described below:
- Navigation graph: An XML resource that contains all navigation-related information in one centralized location. This includes all of the individual content areas within your app, called destinations, as well as the possible paths that a user can take through your app.
- NavHost: An empty container that displays destinations from your navigation graph. The Navigation component contains a default NavHost implementation, NavHostFragment, that displays fragment destinations.
- NavController: An object that manages app navigation within a NavHost. The NavController orchestrates the swapping of destination content in the NavHost as users move throughout your app.
The Navigation component provides a number of other benefits, including the following:
Kotlin:
Fragment.findNavController()
View.findNavController()
Activity.findNavController(viewId: Int)
Java:
NavHostFragment.findNavController(Fragment)
Navigation.findNavController(Activity, @IdRes int viewId)
Navigation.findNavController(View)
- Handling fragment transactions.
- Handling Up and Back actions correctly by default.
- Providing standardized resources for animations and transitions.
- Implementing and handling deep linking.
- Including Navigation UI patterns, such as navigation drawers and bottom navigation, with minimal additional work.
- Safe Args - a Gradle plugin that provides type safety when navigating and passing data between destinations.
- ViewModel support - you can scope a ViewModel to a navigation graph to share UI-related data between the graph's destinations.
Where to put the navigation XML file in a multi-module project?
Create a navigation graph
Navigation occurs between your app's destinations—that is, anywhere in your app to which users can navigate. These destinations are connected via actions.
A navigation graph is a resource file that contains all of your destinations and actions. The graph represents all of your app's navigation paths.
Figure 1 shows a visual representation of a navigation graph for a sample app containing six destinations connected by five actions. Each destination is represented by a preview thumbnail, and connecting actions are represented by arrows that show how users can navigate from one destination to another.
To add a navigation graph to your project, do the following:
A navigation graph is a resource file that contains all of your destinations and actions. The graph represents all of your app's navigation paths.
Figure 1 shows a visual representation of a navigation graph for a sample app containing six destinations connected by five actions. Each destination is represented by a preview thumbnail, and connecting actions are represented by arrows that show how users can navigate from one destination to another.
Add caption |
- Destinations are the different content areas in your app.
- Actions are logical connections between your destinations that represent paths that users can take.
To add a navigation graph to your project, do the following:
- In the Project window, right-click on the res directory and select New > Android Resource File. The New Resource File dialog appears.
- Type a name in the File name field, such as "nav_graph".
- Select Navigation from the Resource type drop-down list, and then click OK.
Navigate to a destination
Navigating to a destination is done using a NavController, an object that manages app navigation within a NavHost. Each NavHost has its own corresponding NavController. You can retrieve a NavController by using one of the following methods:
Kotlin:
Fragment.findNavController()
View.findNavController()
Activity.findNavController(viewId: Int)
Java:
NavHostFragment.findNavController(Fragment)
Navigation.findNavController(Activity, @IdRes int viewId)
Navigation.findNavController(View)
Comments
Post a Comment