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:

dependencies {
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"
}



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"

Alternatively, to generate Kotlin code suitable for Kotlin-only modules add:

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.




Using Navigation Architecture Component in a large app


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.


The Navigation component consists of three key parts that are described below:

  1. 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.
  2. NavHost: An empty container that displays destinations from your navigation graph. The Navigation component contains a default NavHost implementation, NavHostFragment, that displays fragment destinations.
  3. 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.
As you navigate through your app, you tell the NavController that you want to navigate either along a specific path in your navigation graph or directly to a specific destination. The NavController then shows the appropriate destination in the NavHost.

The Navigation component provides a number of other benefits, including the following:
  1. Handling fragment transactions.
  2. Handling Up and Back actions correctly by default.
  3. Providing standardized resources for animations and transitions.
  4. Implementing and handling deep linking.
  5. Including Navigation UI patterns, such as navigation drawers and bottom navigation, with minimal additional work.
  6. Safe Args - a Gradle plugin that provides type safety when navigating and passing data between destinations.
  7. 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.

Add caption

  1. Destinations are the different content areas in your app.
  2. 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:
  1. In the Project window, right-click on the res directory and select New > Android Resource File. The New Resource File dialog appears.
  2. Type a name in the File name field, such as "nav_graph".
  3. 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

Popular posts from this blog

Android Interview question

A complete guide to learn Android Development