Dagger v2: Introducing 2 different areas into one object

I have installed moduleA as the Singleton provider of a single application, ModuleB as the provider of user-related objects.

My fragment of the user screen will use the bus with the system bus to send a message to other users and to display the object associated with the user.

The problem cannot insert another scrope class into one object. Using component.getX works fine, but injecting is preferable. Error message: @UserScope cannot reference bindings with difference areas: @Provides @Singleton Bus ModuleA.provideBus ()

@Module public class ModuleA { @Provides @Singleton Bus provideBus() {...} } 

Module B Information Provider Information Provider

 @Module public class ModuleB{ private final User user; public ModuleB(User user) {...} @Provides @UserScope User provideUser() {} @Provides @UserScope UserManager provideUserManager() {} } 

Configure the components as follows:

 @Component (modules={ModuleA.class}) @Singleton public interface ComponentA { Bus getBus(); void inject(ClassA target); } @Component(modules={ModuleB.class}) @UserScope public interface ComponentB { User getUser(); UserManager getUserManager(); void inject(ClassA target); } class UserFragment exrtends Fragment { @Inject Bus bus; @Inject UserManager userManager; public void onCreate() { getComponentA().inject(this); getComponentB().inject(this); } } 
+6
source share
2 answers

Try this configuration, it works for me. There is really a lack of good documentation about Dagger2, so I studied a few open source code examples that you can find on GitHub, etc. By keyword, for example Dagger2.

Application Level Component

 @Singleton @Component(modules = AppModule.class) public interface AppComponent { // exported for child-components Bus eventBus(); } 

Application level module

 @Module public class AppModule { @Provides @Singleton Bus provideBus() { return BusProvider.getInstance(); } } 

Activity Level Component

 @ActivityScope @Component(dependencies=AppComponent.class, modules=MainActivityModule.class) public interface MainActivityComponent { void inject( MainActivity mainActivity ); } 

Activity Level Module

 @Module public class MainActivityModule { private final MainActivity mActivity; public MainActivityModule( MainActivity activity ) { mActivity = activity; } @Provides MainActivityTitleController provideTitleController() { return new MainActivityTitleController( mActivity ); } } 

Android application class

 public class MyApplication extends Application { private AppComponent mAppComponent; @Override public void onCreate() { super.onCreate(); // Dagger2 mAppComponent = Dagger_AppComponent.builder() .appModule( new AppModule( this )) .build(); } public AppComponent getComponent() { return mAppComponent; } public static AppComponent getComponent( Context context ) { return ((MyApplication)context.getApplicationContext()).getComponent(); } } 

And finally, Activity

 public class MainActivity extends ActionBarActivity { // Injectable fields @Inject Bus mEventBus; @Inject MainActivityTitleController mTitleController; private MainActivityComponent mComponent; @Override protected void onCreate( Bundle savedInstanceState ) { // Dagger2 mComponent = Dagger_MainActivityComponent.builder() .appComponent( ((MyApplication)getApplication()).getComponent() ) .mainActivityModule( new MainActivityModule( this ) ) .build(); mComponent.inject( this ); } } 
+20
source

I think the main problem in your code snippets that you have indicated is that your ModuleB must be dependent on ModuleA in order to correctly provide the singleton error you were receiving. That is, this should work:

 @Component(modules={ModuleB.class}, dependencies = ComponentA.class) @UserScope public interface ComponentB { User getUser(); UserManager getUserManager(); void inject(MainActivity target); } 

I recreated your classes and made some assumptions to fill in the blanks, and it seems to work fine. You can see the full working code here on GitHub . The only difference in my code is what you called ClassA / UserFragment I just called MainActivity , but otherwise the structure is the same.

+7
source

Source: https://habr.com/ru/post/983760/


All Articles