title: How to set up dependency injection using Dagger for things other than Activities and Fragments?
body: <p>I started setting up dependency injection using <a href="http://square.github.io/dagger/" rel="noreferrer">Dagger</a> as follows. Please feel encouraged to correct my implementation since I might have mistakes in there! The implementation follows the <a href="https://github.com/square/dagger/tree/dagger-parent-1.1.0/examples/android-simple" rel="noreferrer">android-simple example</a> provided by the project. In the following you can see how I successfully added dependency injection for <code>Activities</code> and <code>Fragments</code>. I try to keep it easy for now so I decided to inject <a href="https://github.com/JakeWharton/timber" rel="noreferrer">Timber</a> as a logger substitution for <a href="http://developer.android.com/reference/android/util/Log.html" rel="noreferrer">Android's log util</a>.</p>
  
  <pre><code>import android.app.Application;
  import java.util.Arrays;
  import java.util.List;
  import dagger.ObjectGraph;
  import com.example.debugging.LoggingModule;
  
  public class ExampleApplication extends Application {
  
      private ObjectGraph mObjectGraph;
  
      protected List&lt;Object&gt; getModules() {
          return Arrays.asList(
                  new AndroidModule(this),
                  new ExampleModule(),
                  new LoggingModule()
          );
      }
  
      private void createObjectGraphIfNeeded() {
          if (mObjectGraph == null) {
              Object[] modules = getModules().toArray();
              mObjectGraph = ObjectGraph.create(modules);
          }
      }
  
      public void inject(Object object) {
          createObjectGraphIfNeeded();
          mObjectGraph.inject(object);
      }
  }
  </code></pre>
  
  <p>By now the <a href="https://github.com/square/dagger/blob/dagger-parent-1.1.0/examples/android-simple/src/main/java/com/example/dagger/simple/AndroidModule.java" rel="noreferrer"><code>AndroidModule</code></a> is not used anywhere it but might be helpful when a <code>Context</code> and <code>LayoutInflater</code> is needed e.g. in <code>CursorAdapters</code>.</p>
  
  <pre><code>import android.content.Context;
  import android.view.LayoutInflater;
  import javax.inject.Singleton;
  import dagger.Module;
  import dagger.Provides;
  
  /**
   * A module for Android-specific dependencies which require a {@link Context} 
   * or {@link android.app.Application} to create.
   */
  @Module(library = true)
  public class AndroidModule {
      private final ExampleApplication mApplication;
  
      public AndroidModule(ExampleApplication application) {
          mApplication = application;
      }
  
      /**
       * Allow the application context to be injected but require that it be 
       * annotated with {@link ForApplication @Annotation} to explicitly 
       * differentiate it from an activity context.
       */
      @Provides @Singleton @ForApplication Context provideApplicationContext() {
          return mApplication;
      }
  
      @Provides @Singleton LayoutInflater provideLayoutInflater() {
          return (LayoutInflater) mApplication
              .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      }
  }
  </code></pre>
  
  <p>I am not sure what <em>application-specific providers</em> would go here. I stay with <em>logging</em> for now.</p>
  
  <pre><code>import dagger.Module;
  
  @Module(
          complete = false
  )
  public class ExampleModule {
      public ExampleModule() {
           // TODO put your application-specific providers here!
      }
  }
  </code></pre>
  
  <p>I prepared <code>LoggingModule</code> which provides access to <em>Timber</em>.</p>
  
  <pre><code>package com.example.debugging;
  
  import javax.inject.Singleton;
  
  import dagger.Module;
  import dagger.Provides;
  import com.example.BuildConfig;
  import com.example.activities.BaseFragmentActivity;
  import com.example.activities.DetailsActivity;
  import com.example.fragments.BaseListFragment;
  import com.example.fragments.ProfilesListFragment;
  import timber.log.Timber;
  
  @Module(injects = {
          // Activities
          BaseFragmentActivity.class,
          DetailsActivity.class,
          // Fragments
          BaseListFragment.class,
          ProfilesListFragment.class
  })
  public class LoggingModule {
  
      @Provides @Singleton Timber provideTimber() {
          return BuildConfig.DEBUG ? Timber.DEBUG : Timber.PROD;
      }
  }
  </code></pre>
  
  <p>The base class for <code>Activities</code> injects itself into the object graph ...</p>
  
  <pre><code>package com.example.activities;
  
  import android.os.Bundle;    
  import com.actionbarsherlock.app.SherlockFragmentActivity;    
  import javax.inject.Inject;
  import com.example.ExampleApplication;
  import timber.log.Timber;
  
  public abstract class BaseFragmentActivity extends SherlockFragmentActivity {
  
      @Inject Timber mTimber;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          // ...
          super.onCreate(savedInstanceState);
          ((ExampleApplication) getApplication()).inject(this);
      }
  }
  </code></pre>
  
  <p>... and any sub class benefits from <em>Timber</em> being already present.</p>
  
  <pre><code>package com.example.activities;
  
  import android.os.Bundle;
  import com.example.R;
  
  public class DetailsActivity extends BaseFragmentActivity {
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_details);
          mTimber.i("onCreate");
          // ...
      }
  }
  </code></pre>
  
  <p>Same for <code>Fragments</code>: the base class does the dirty job ...</p>
  
  <pre><code>package com.example.fragments;
  
  import android.os.Bundle;
  import com.actionbarsherlock.app.SherlockListFragment;
  import javax.inject.Inject;
  import com.example.ExampleApplication;
  import timber.log.Timber;
  
  public abstract class BaseListFragment extends SherlockListFragment {
  
      @Inject Timber mTimber;
  
      @Override
      public void onActivityCreated(Bundle savedInstanceState) {
          super.onActivityCreated(savedInstanceState);
          ((ExampleApplication) getActivity().getApplication()).inject(this);
      }
  
  }
  </code></pre>
  
  <p>... and the sub class benefits from its super class.</p>
  
  <pre><code>package com.example.fragments;
  
  import android.os.Bundle;
  
  public class ProfilesListFragment extends BaseListFragment {
  
      @Override
      public void onActivityCreated(Bundle savedInstanceState) {
          super.onActivityCreated(savedInstanceState);
          // TODO This might be a good example to inject resources
          // in the base class. But how?
          setEmptyText(getResources()
             .getString(R.string.profiles_list_no_content));
          mTimber.i("onActivityCreated");
          // ...
      }
  
  }
  </code></pre>
  
  <p>So far so good. But how can inject <em>Timber</em> into <code>BaseCursorAdapter</code>, <code>BaseContentProvider</code>, <code>BaseSQLiteOpenHelper</code>, <code>BaseService</code>, <code>BaseAsyncTask</code> and <code>static</code> helper methods?</p>
  
  <p>The deprecated <a href="https://github.com/christopherperry/dagger/tree/master/examples/android/src/dagger" rel="noreferrer">android example by  Christopher Perry</a> points out how to inject an <a href="https://github.com/christopherperry/dagger/blob/master/examples/android/src/dagger/adapter/StabbedAdapter.java" rel="noreferrer">Adapter</a> into a <a href="https://github.com/christopherperry/dagger/blob/master/examples/android/src/dagger/fragment/StabbedFragment.java" rel="noreferrer">ListFragment</a> but not how to inject <code>Context</code>, <code>Resources</code>, <code>LayoutInflater</code>, <code>Cursor</code> into the <code>(Cursor)Adapter</code> or just <em>Timber</em>.</p>
  
  <hr>
  
  <p>References:</p>
  
  <ul>
  <li><a href="http://square.github.io/dagger/" rel="noreferrer">Dagger</a></li>
  <li><a href="https://github.com/square/dagger/tree/dagger-parent-1.1.0/examples/android-simple" rel="noreferrer">android-simple example</a></li>
  <li><a href="http://www.infoq.com/presentations/Dagger" rel="noreferrer">Jesse Wilson - Dagger: A Fast Dependency Injector for Android and Java</a></li>
  <li><a href="http://www.infoq.com/presentations/Android-Design/" rel="noreferrer">Eric Burke - Android App Anatomy</a></li>
  </ul>