0

I'm using the new Android injection from dagger 2.11, and i have this simple module :

@Module
public class MyModule{

@MyScope
@Provides
SomeClass provideSomeClass(Context context){
    return new SomeClass(context);
}

}

as you can see i need to pass a context to SomeClass constructor. but i don't know how to do this.

without Android injector i can do it like below :

@Module
public class MyModule{

private Context context;

public MyModule(Context context){
    this.context = context;
}

@MyScope
@Provides
Context provideContext(){return context;}

@MyScope
@Provides
SomeClass provideSomeClass(Context context){
    return new SomeClass(context);
}

}

but since i can't access to MyModule with AndroidInjection.inject() i can't pass context to it.

Mahdi Nouri
  • 1,391
  • 14
  • 29

1 Answers1

1

If your module is attached to the application as a singleton, you can use Application Context directly. Or you may want to attach your module to your activity/fragment. In that case, you don't have to pass any parameter to your module because dagger is already passed your activity/fragment to your attached module. I want to show you as a simple sample.

Let say you have MainActivity and MainActivityModule. To get your activity context to your MainActivityModule is that calling AndroidInjection.inject(this); in your MainActivity (before super.onCreate()). Here is that module which has context.

@Module
public class MainActivityModule{

public MainActivityModule(MainActivity mainActivity){
    // you can use your main activity as a context.
}
mertsimsek
  • 266
  • 2
  • 10
  • thank you for your answer, but i want to use MyModule for several activities and i need their base context. – Mahdi Nouri Sep 09 '17 at 11:05
  • You can create your module as a singleton and you can pass application context to your module when you inject into it. – mertsimsek Sep 09 '17 at 11:07
  • are you sure that this is a good practice? btw i need base context not application context. – Mahdi Nouri Sep 09 '17 at 11:16
  • It depends on what you want to do. getBaseContext() is not a good practice. Better you use getApplicationContext or Activity/Fragment context. At least I know what I’m holding on to and that I’m holding a reference to something that can cause a memory leak. So why do you insist on that? – mertsimsek Sep 09 '17 at 11:24
  • I meant activity context, i thought baseContext and activity context are the same :D – Mahdi Nouri Sep 09 '17 at 11:26
  • If you need a context that you want to use with multiple activities, you should use application context. – mertsimsek Sep 09 '17 at 11:29
  • but i want to change the behavior of SomeClass based on the activity context – Mahdi Nouri Sep 09 '17 at 11:32
  • What exactly do you want to change according to the activity context? – mertsimsek Sep 09 '17 at 11:43
  • Its some UI stuff, and since there is separate layout for each activity i need to change some behavior based on the context – Mahdi Nouri Sep 09 '17 at 11:50