android / architecture-components-samples

Samples for Android Architecture Components.
https://d.android.com/arch
Apache License 2.0
23.44k stars 8.28k forks source link

How to use navigation components with multi-Module application and BottomNavigationView #663

Open LLin233 opened 5 years ago

LLin233 commented 5 years ago

https://stackoverflow.com/questions/54616996/android-navigation-component-and-bottomnavigationview-in-a-multi-module-application

I am facing extract same circumstance here... trying to create a multi-module application where every feature is a standalone library (module). Single activity pattern is applied throughout the project, created different navGraphs for different modules and included them in app's navGraph, but can't make it work.

could anyone give me some ideas? Thank you!

damienlo commented 5 years ago

@LLin233 You can create a module "navigation" and move your graph(s) there. References to Fragments will be in highlighted red but it compiles and it works.

All your feature modules can take that navigation as dependency in order to access the safeArgs (if you are using them) and the actions.

hamedsilver commented 4 years ago

@LLin233 check my sample https://github.com/hamedsilver/ArchApp

i-am-mani commented 4 years ago

@damienlo is this method still the preferred way? or any other better options are available now?

engi2nee commented 3 years ago

as an addition to @damienlo fix, you can do a "gradle hack" to share the resources between the navigation module and the app module (assuming it has access to all feature modules), this way the fragment names won't be highlighted as red. credits to @eyal-lezmy in this talk (skip to 44:00): https://www.droidcon.com/media-detail?video=334638906

  1. First create a new package under main in your navigation module called "sharedRes", and move the navigation resource folder with the navigation graphs xml into it.
  2. in the app's build.gradle add android { sourceSets.debug.res.srcDirs += "$rootDir/navigation/src/main/sharedRes"}}
  3. in the navigation module's build.gradle add android { sourceSets.main.res.srcDirs += "$rootDir/navigation/src/main/sharedRes" }
  4. clean build and rebuild.
eyal-lezmy commented 3 years ago

If I remember well this hack is not "working" anymore with the rendent versions of Android Studio. But in our project we end up using a symbolic link to duplicate the file in both modules.

engi2nee commented 3 years ago

@eyal-lezmy weird, it is working correctly on our project, using the latest version of navigation component library and android studio 4.1.1.

P1NG2WIN commented 3 years ago

@engi2nee Can you explaine what does it do?

rhlmshr commented 3 years ago

@P1NG2WIN Refer the video at 44:00. It is discussed what it does behind the scenes.

tugceaktepe commented 1 year ago

damienlo I created navigation module and move all nav graphs there. Yes compilation it's ok but it gives runtime error like this:


 Caused by: android.view.InflateException: Binary XML file line #52 in com.mypackage:layout/activity_main: Binary XML file line #52 in com.mypackage:layout/activity_main: Error inflating class androidx.fragment.app.FragmentContainerView
                                                                                                    Caused by: android.view.InflateException: Binary XML file line #52 in com.mypackage:layout/activity_main: Error inflating class androidx.fragment.app.FragmentContainerView
                                                                                                    Caused by: androidx.fragment.app.Fragment$InstantiationException: Unable to instantiate fragment com.mypackage.login.splash.SplashFragment: make sure class name exists

Android Studio Flamingo 2022.2.1 Patch 2 AGP version 8.0