First time using Dagger2.
In my android application I have a MyApplication class that extends Application. I also have an ImageAssistant class that is a collection of related image-processing methods. In my MyApplicaiton class I used to instantiate an ImageAssistant for all the activities to use.
Now I am trying to make it work with Dagger2, but I dont know how to pass a context in the module that provides ImageAssistant
This is how my code looked:
public class ImageAssistant {
Context context;
public ImageAssistant(Context context){
this.context = context;
}
// A bunch of methods...
}
public class MyApplication extends Application {
public ImageAssistant imageAssistant;
public void onCreate() {
imageAssistant = new ImageAssistant(this);
}
}
Now, enter Dagger 2, here is what I have
public class ImageAssistant {
Context context;
@Inject
public ImageAssistant(Context context){
this.context = context;
}
// A bunch of methods...
}
public class MyApplication extends Application {
@Inject
public ImageAssistant imageAssistant;
public void onCreate() {
}
}
in package .modules:
AppModule.java
@Module
public class AppModule {
@Provides
ImageAssistant provideImageAssistant() {
return new ImageAssistant(); // HERE A CONTEXT IS NEEDED. WHERE TO GET IT FROM?
}
}
EDIT: This is how my module looks now, but I still dont know how to tie everything together:
@Module
public class AppModule {
private MyApplication application;
public AppModule(MyApplication application) {
this.application = application;
}
@Provides
Context provideApplicationContext() {
return this.application;
}
@Provides
ImageAssistant provideImageAssistant(ImageAssistant imageAssistant) {
return imageAssistant;
}
}
And this is the AppComponent:
@Singleton
@Component(modules = {AppModule.class})
public interface AppComponent {
ImageAssistant provideImageAssistant();
Context context();
}