Android Dagger 2.10 or higher - void inject X @Bindsinstance?

Asked

Viewed 48 times

2

I have a big question about the correct use of Dagger 2.10 or higher for android implementing according to the specifications of the Dagger documentation for Android.

Example code:

@Singleton
        @Component(modules = {
                AndroidInjectionModule.class,
                ApplicationModulo.class,
                ViewBuilderModule.class
                })
        public interface FipeApplicationComponent extends AndroidInjector<FipeApplication>{


            @Component.Builder
            interface Builder{
                @BindsInstance
                FipeApplicationComponent.Builder application(FipeApplication application);

                @BindsInstance
                FipeApplicationComponent.Builder urlBase(String urlBase);

                FipeApplicationComponent build();
            }

            void inject(FipeApplication app);

        }
  1. Bindsinstance

In the past when we wanted to link some instance of some class that needed to be instantiated outside the Dagger domains we passed this instance by the constructor. Now it is suggested to use @Bindsinstance within a @Component.Builder interface builder

  1. void inject

I see in some examples on the internet the use of void inject(Fipeapplication app) and as the previous item I asked here already has a:

...    
@Component.Builder
                interface Builder{
                    @BindsInstance
                    FipeApplicationComponent.Builder application(FipeApplication application);
...

1 answer

0

My doubt was when to use Bindsinstance vs Inject in the Component.

They’re actually two different things:

The Bindsinstance serves to link some instance within the Dagger domains that is not in the tree, something that needs to come from outside already instantiated. A Context for example.

The void inject(Myclassxxx myObjectXXX) serves to indicate where Dagger will be "authorized" to inject the dependencies.

void inject(Mainactivity mainActivity);

void inject(Secondactivity secondActivity);

void inject(Presenter presenter);

So within Mainactivity, Secondactivity or Presenter we can do:

.... @Inject protected Context mContext; ...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.