When to use void Inject on Dagger

Asked

Viewed 43 times

1

Dagger, can someone give me a hand?

I managed to do some 3 very simple projects always trying several to have new problems and learn more, but one part I’m kind of copying because I don’t understand, in the Component there are some examples that show something like:

void inject(MainActivity activity);

or

void inject(BaseView mView); //Eu sei que o é BaseView e para que serve!!

A doubt, for either the Inject. My doubt is partly because in other examples there is and often there is also a:

Retrofit exposeRetrofit();

or

Context exposeContext();

I read the documentation but could not see how it works. When I use void inject(...); and when I use Objeto exposeObjeto();

Does anyone know?

1 answer

0

When you put void inject(MainActivity activity); is defining that class Mainactivity will be able to use the instances of the objects that were defined in the Module. If you needed to use the same component in a Fragment you would also need to define that method there void inject(MeuFragment fragment);.

When you define Retrofit exposeRetrofit(); is exposing the object Retrofit that you defined in the Module, so that your Activity or Fragment can use it through the @Inject Retrofit retrofit;.

I hope I’ve helped.

  • But because in some projects even when these provide are used and in the Component there is no void inject(xxx); ??

Browser other questions tagged

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