Nimrodda / dagger-androidinjector

This sample is part of a tutorial on how to use the new dagger-android module, which was released in Dagger 2.10.
https://medium.com/@nimroddayan/android-and-dagger-2-10-androidinjector-5e9c523679a3
135 stars 22 forks source link

Feature with multiple activities #1

Open juliocbcotta opened 7 years ago

juliocbcotta commented 7 years ago

Hi, Would you be willing to show how to create a feature with multiple activities and a resource shared only with the activities of this feature ?

Nimrodda commented 6 years ago

Dagger Android is designed in a specific way where Application holds the top most level component that knows about Android components such as Activity, Service, etc. When AndroidInjection.inject(this) is called in an Activity, Dagger will try to get instance of AndroidInjector<Activity> from the Application class, which is injected by the Application Component normally.

So we're left with the following hierarchy of Dagger components: Application Component -> Activity Sub Component -> Fragment Sub Component

What you're asking for is not possible in the above hierarchy, which is the common hierarchy Dagger Android is set up. You might be able to stick in another component between Application component and Activity component, but it will basically be a hack. An example of such hack is available here.

I'd strongly recommend against hacks and instead change your UI architecture to use multiple Fragments under the same Activity to achieve what you want. It is possible to do it with Fragments since they support nesting of Fragments. So you can have the following hierarch with fragments:

Application Component -> Activity Sub Component -> Fragment Sub Component -> Fragment Sub Component -> and so on.