title: Dagger2 module: how to obtain context to pass to the constructor of a class I want to provide
body: <p>First time using Dagger2.</p>
  
  <p>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.</p>
  
  <p>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</p>
  
  <p>This is how my code looked:</p>
  
  <pre><code>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);
      }
  
  }
  </code></pre>
  
  <p>Now, enter Dagger 2, here is what I have</p>
  
  <pre><code>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() {
  
  
      }
  
  }
  </code></pre>
  
  <p>in package .modules:</p>
  
  <pre><code>AppModule.java
  
  
  @Module
  public class AppModule {
  
      @Provides
      ImageAssistant provideImageAssistant() {
          return new ImageAssistant(); // HERE A CONTEXT IS NEEDED. WHERE TO GET IT FROM?
      }
  
  }
  </code></pre>
  
  <p>EDIT: This is how my module looks now, but I still dont know how to tie everything together:</p>
  
  <pre><code>@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;
      }
  
  }
  </code></pre>
  
  <p>And this is the AppComponent:</p>
  
  <pre><code>@Singleton
  @Component(modules = {AppModule.class})
  public interface AppComponent {
  
      ImageAssistant provideImageAssistant();
      Context context();
  
  }
  </code></pre>