Androidx Navigation 2.3.0 adds navigation support for dynamic feature Modules, so we use this to separate multiple function modules for modularization
Dynamic feature Module that is basically not used in China
Android App Bundle is an official dynamic distribution scheme launched in 2018, which is similar to various domestic plug-in schemes. However, it requires Google Play Store support, which makes it unavailable in China
Since the navigation component supports navigation between dynamic feature modules, we can use dynamic feature Module to split functional modules to achieve modularization
The traditional split scheme is like this: Feature modules are isolated from each other, and App Module depends on each feature Module indirectly on base library, public library
The structure of dynamic feature Module is as follows
Dynamic Feature Modules can also be installed on demand, that is, they may not be included in the APK initially downloaded by the user, but installed at run time. We can include them directly in APK
Use the Dynamic Feature Module
First we introduce dependencies in base Lib
dependencies {
def nav_version = "2.3.0 - alpha06"
api "androidx.navigation:navigation-fragment-ktx:$nav_version"
api "androidx.navigation:navigation-ui-ktx:$nav_version"
api "androidx.navigation:navigation-dynamic-features-fragment:$nav_version" } Copy the code
We create main_nav. XML in the RES /navigation directory in the App Module
Next we set the default host in activity_main
❝
DynamicNavHostFragment is used instead of NavHostFragment
❞
Direct jump fragment
Let’s create a Dynamic feature module called Feature1
❝
The preceding part of the package name of the Dynamic feature Module must be the same as the applicationId of the app Module package name. Otherwise, subsequent include operations may fail
❞
Here we chose to integrate the Module at installation time
Then we created a fragment under this Module called Feature1OneFragment
We then import the fragment directly into main_nav. XML and add the action
Then you can open Feature1OneFragment under the App’s MainFragment
❝
In my demo, feature2 introduced a fragment directly, so it jumped to Feature2OneFragment
❞
Jump to activity
Create an activity in Feature1 (Feature2 in the demo)
ModuleName also needs to be specified
Use graph inside the Dynamic Feature Module
We can configure a navigation Graph for the Dynamic Feature Module separately to handle jumps within the Dynamic feature Module
Create feature1_nav. XML in Feature1, where startDestination is Feature1OneFragment
In main_nav. XML we need to use the graph in a different way
We use a new tag called include-dynamic, and we see several unused attributes
graphPackage
为dynamic feature module
The package namegraphResName
为dynamic feature module
The name of the inner graphmoduleName
For the module name
❝
Note: The graphPackage here can be omitted
- An exception in the graphId cannot be found if the module package name is not configured in the format described above
- The id of the include-dynamic tag must be the same as that of the
feature1_nav.xml
The id in the navigation TAB is consistent, or the id is not set in the latter TAB❞
Once you navigate from the App Module to Feature1’s startDestination, you can use its internal logic for subsequent navigation
Description Switch between feature modules
The Navigation component does not currently support the deep link of Dynamic Include Graph
Therefore, I have not found a particularly elegant way at present, and the known solutions are as follows
- reflection
- Use the ServiceLoader
- Using dependency Injection
demo
About me
I am a Fly_with24
-
The Denver nuggets
-
Jane’s book
-
Github