architecture-components-samples
architecture-components-samples copied to clipboard
[NavigationAdvancedSample] Support for loading navgraph in Dynamic feature module
Hi I am getting error when setting navgraph with the <include-dynamic tag which contains the navgraph within the dynamic feature module.
<include-dynamic
android:id="@+id/someFeatureNav"
app:moduleName="someFeatureModule"
app:graphResName="some_navigation"
app:graphPackage="com.some.package" />
Passing the id of the navigation graph. R.navigation.included_navigation into setupWithNavController.
Error points to NavHostFragment.create
// Otherwise, create it and return it. val navHostFragment = NavHostFragment.create(navGraphId) fragmentManager.beginTransaction() .add(containerId, navHostFragment, fragmentTag) .commitNow()
Caused by: java.lang.IllegalStateException: Could not find Navigator with name "include-dynamic". You must call NavController.addN avigator() for each navigation type. E at androidx.navigation.NavigatorProvider.getNavigator(NavigatorProvider.java:98) E at androidx.navigation.NavInflater.inflate(NavInflater.java:107) E at androidx.navigation.NavInflater.inflate(NavInflater.java:141) E at androidx.navigation.NavInflater.inflate(NavInflater.java:88) E ... 31 more
As NavHostFragment.create() returns NavHostFragment instead of DynamicNavHostFrqagment, so can't directly cast the result to DynamicNavHostFragment.
Found similar question here with a workaround by having a custom createDynamicNavHostFragment.
Is there any cleaner way of doing this?