Darish
Darish

Reputation: 11481

Navigate from one dynamic feature module to another with Jetpack navigation architecture component

How to navigate from the base module or a dynamic module to a dynamic module using the navigation component?

How to define the destination module inside the navigation graph?

Upvotes: 0

Views: 1416

Answers (1)

Darish
Darish

Reputation: 11481

Android Studio 4.0 introduces the dynamic feature module with navigation graph support.

To use the dynamic feature module inside the navigation graph, you need to use Android Studio 4.0 or above.

Step 1:

In your main app/build.gradle, define the feature module dependency

    android {
    ...
    dynamicFeatures = [":camera", ":video", ":payment"]
   }

Step 2:

In the feature module build.gradle files, define its module dependencies. Note that one dynamic feature module can depend on another dynamic feature module.

In your payment/build.gradle

dependencies {
implementation project(':app')
}

In your camera/build.gradle

dependencies {
implementation project(':app')
}

In your video/build.gradle

dependencies {
implementation project(':app')
implementation project(':camera')
}

Step 3:

You need to enable the feature.on.feature flag for the Android studio.

  1. Go to Help > Edit Custom VM Options
  2. add rundebug.feature.on.feature to the file, save the change, and restart Android Studio.

    enter image description here

  3. Run > Edit configurations and in Run/Debug Configuration you can define various installation configurations for testing.

enter image description here

Step 4:

You need to use the DynamicNavHostFragment instead of the NavHostFragment

<fragment
  android:id="@+id/nav_host_fragment"
  android:name="androidx.navigation.dynamicfeatures.
  fragment.DynamicNavHostFragment"
  app:navGraph="@navigation/nav_graph"
  … />

Step 5:

Define your nav graph. Here you can use the attribute app:moduleName to define the module dependency.

    <navigation>
  <fragment
    app:moduleName="featureA"
    android:name="full.path.to.MyFragment"/>
  <activity
    app:moduleName="featureB"
    android:name="full.path.to.MyActivity"/>
</navigation>

The navigation library will do the rest for you.

Happy coding :)

Upvotes: 1

Related Questions