title: Activity Field Injection Using Dagger 2
body: <p>When using <strong>Constructor Injection</strong> with Dagger2, I can really see how the concept of dependency injection is implemented:</p>
  
  <pre><code> public class Dependent {
  
       @Inject
       public Dependent(Dependency dependency) {
           // We're dependent on an instance of the Dependency class
           // but we don't care who provides it
       }
  
   }
  </code></pre>
  
  <p>But when it comes to an <code>Activity</code>, because Android is instantiating it for us, we need to use <strong>Field Injection</strong> to satisfy our dependencies. All of the examples I found online suggest something like this:</p>
  
  <ol>
  <li>Create a <code>@Module</code> class to provide our dependency.</li>
  <li>Create a <code>@Component</code> interface and call the generate builder to instantiate it. Most of the examples perform this in the <code>Application</code> class and save a reference as a member.</li>
  <li>In our <code>Activity</code> - create our <code>@Inject</code> field, and in the <code>onCreate(..)</code> method, get a reference to the <code>Component</code> from our <code>Application</code> and initiate the injection.</li>
  </ol>
  
  <p>My problem with this approach is that it doesn't <em>feel</em> loosely coupled, which is what we're trying to achieve.</p>
  
  <pre><code> public class DependentActivity extends Activity {
  
       @Inject Dependency mDependency;
  
       public void onCreate(final Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           ((MyApplication) getApplication()).getComponent().inject(this);
  
           // We're dependent on an instance of dependency
           // but now we not only know who provides it,
           // we explicitly initiate the injection
       }
  
   }
  </code></pre>
  
  <p>What am I missing?</p>