architecture-components-samples icon indicating copy to clipboard operation
architecture-components-samples copied to clipboard

[NavigationAdvancedSample] Support for loading navgraph in Dynamic feature module

Open tieren1 opened this issue 5 years ago • 0 comments
trafficstars

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?

tieren1 avatar Oct 08 '20 05:10 tieren1