androidandroid-fragmentsandroid-architecture-componentsandroid-mvvmmodularization

How to get appModule properties in feature modules


My project is having feature1Module and feature2Module. These 2 are included in app module build gradle file.

implementation project(":feature1Module")
implementation project(":feature2Module")

I can access feature1Module and feature2Module's on app module but not able to access app module from feature1Module module.

Ex:- app has DashboardFragment

feature1Module has Feature1Fragment

feature2Module has Feature2Fragment

I can navigate DashboardFragment to Feature1Fragment and Feature2Fragment but not able to navigate to DashboardFragment from Feature1Fragment beacause i cannot access DashboardFragment from Feature1Fragment.

Please suggest how to achieve this.


Solution

  • Feature modules cant depend to app module. You need to create a common module ex common, then make app and feature module depend on it.

    Suggestion

    common has interface Router

    interface Router {
      fun moveToDashboard()
    }
    

    app module

    class AppRouter : Router {
      override fun moveToDashboard() {
        // your logic here
      }
    }
    

    feature module, call to common

    private val router: Router = // init logic, maybe call setter from app
    
    router.moveToDashboard()