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

2

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

Sample 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);

        }
  • BindsInstance
  • Previously, when we wanted to bind some instance of a class that needed to be instantiated outside the dagger domains we would pass this instance to the constructor. It is now suggested to use @BindsInstance within a @ Component.Builder         interface Builder

  • void inject
  • I see in some examples on the internet the use of void inject (FipeApplication app) being that according to the previous item that I asked here already has one:

    ...    
    @Component.Builder
                    interface Builder{
                        @BindsInstance
                        FipeApplicationComponent.Builder application(FipeApplication application);
    ...
    
        
    asked by anonymous 16.01.2018 / 14:10

    1 answer

    0

    My question was when to use BindsInstance vs Inject in the Component.

    Actually they are two different things:

    BindsInstance serves to bind some instance within the Dagger domains that are 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 the Dagger will be "authorized" to inject the dependencies.

    void inject (MainActivity mainActivity);

    void inject (SecondActivity secondActivity);

    void inject (Presenter presenter);

    So within the MainActivity, SecondActivity or Presenter we can do:

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

        
    05.06.2018 / 02:59