Reputation: 11481
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
Reputation: 11481
To use the dynamic feature module inside the navigation graph, you need to use Android Studio 4.0 or above.
In your main app/build.gradle, define the feature module dependency
android {
...
dynamicFeatures = [":camera", ":video", ":payment"]
}
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')
}
You need to enable the feature.on.feature
flag for the Android studio.
add rundebug.feature.on.feature to the file, save the change, and restart Android Studio.
Run > Edit configurations and in Run/Debug Configuration you can define various installation configurations for testing.
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"
… />
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