Kuanysh Raimbekov
Kuanysh Raimbekov

Reputation: 446

Sharing objects between only two activities using Dagger 2.0

I have multiple activities extending BaseActivity. Each activity has own component and module. Those components extend BaseActivityComponent which has own BaseActivityModule. Now I have two activities that I want to be able to inject objects that other activities will not be able to.

How can I do that?

I tried creating a base component and base module just for those two activities, but that gave me error:

MyModule depends on a scoped components in a non-hierarchical order

Now I want AdvertAdvanced to be shared by two activities.

This component is used by one of the two activities:

@ActivityScope
@Component(
        dependencies = ApplicationComponent.class,
        modules = {
                BaseActivityModule.class,
                MainActivityModule.class
        }
)
public interface MainActivityComponent extends BaseActivityComponent {
        void inject(MainActivity mainActivity);
        MainActivityPresenter mainPresenter();
        AdvertAdvanced advertAdvanced();
}

As base component for each activity I have the following:

    @ActivityScope
    @Component(dependencies = ApplicationComponent.class, modules = BaseActivityModule.class)
    public interface BaseActivityComponent extends ApplicationComponent {

        BaseActivity baseActivity();
        Navigator navigator();
        KeyboardAction keyboardAction();

    }

Application component:

    @Singleton
    @Component(
            modules = {
                    ApplicationModule.class
            }
    )
    public interface ApplicationComponent {

        void inject(BaseActivity activity);

        Context context();
        ApiService apiService();
        Picasso picasso();

    }

This is how a module for activities looks like:

@Module
public class MainActivityModule {

    private AdvertAdvanced advertAdvanced;

    public MainActivityModule() {
        advertAdvanced = new AdvertAdvanced();
    }

    @Provides
    @ActivityScope
    MainActivityPresenter provideActivityPresenter(****) {
        return ****;
    }

    @Provides
    @ActivityScope
    AdvertAdvanced advertAdvanced() {
        return advertAdvanced;
    }

}

Upvotes: 2

Views: 2173

Answers (1)

EpicPandaForce
EpicPandaForce

Reputation: 81578

You will need to define a new component AdvertActivityComponent, with a component dependency of BaseActivityComponent, with a subscope such as @AdvertisedScope. Your current BaseActivityComponent should have its own scope, such as @BaseScope, and your MainActivityComponent should depend on AdvertActivityComponent with a @ConcreteActivityScope.

Essentially, your scope hierarchy is:

  • Application scope (@Singleton)
  • Base Activity scope (@BaseScope)
  • Advert Activity scope (@AdvertScope)
  • Main Activity scope (@ConcreteScope)

And your components should extend one another and depend on their supercomponent as such.

Upvotes: 1

Related Questions