title: Dagger for Android: Injecting an Activity to the object graph after calling setContentView
body: <p>I want to use Dagger on Android to inject an instance of an <code>Activity</code> into another class as follows:</p>
  <pre><code>class PresentationLayer{
      Activity mActivity;
      
      @Inject
      public PresentationLayer(Activity activity){
          this.mActivity = activity;
      }
      
      public void presentData(User user){
          ((TextView)mActivity.findViewById(R.id.username))
              .setText(user.getName());
          //...
          //...
      }
  }
  </code></pre>
  <p>I am able to do the injection, but all fields of the <code>Activity</code> are <strong><code>null</code> at the time of injection</strong>.</p>
  <p>Here's how I am doing the injection:</p>
  <p>My <code>Activity</code> is a module in itself.</p>
  <pre><code>@Module(
      complete = false
  )
  class MainActivity extends Activity{
      
      @Override
      public void onCreate(Bundle bundle){
          super.onCreate(bundle);
          setContentView(R.layout.main_activity);
          ObjectGraph objectGraph = CustomApplication.getObjectGraph();
          PresentationLayer pres = objectGraph.get(PresentationLayer.class);
      }
      
      @Provides Activity provideActivity(){
          return this;
      }
      
      
  }
  </code></pre>
  <p>This is my main module</p>
  <pre><code>@Module(
      entryPoints = PresentationLayer.class,
      includes = MainActivity.class
  )
  class DaggerModule{
      @Provides PresentationLayer providePresentation(Activity activity){
          return new PresentationLayer(activity);
      }
  }
  </code></pre>
  <p>And my <code>Application</code> class that bootstraps the object graph.</p>
  <pre><code>class CustomApplication extends Application{
  
      private static ObjectGraph sObjectGraph;
      @Override
      public void onCreate(){
          sObjectGraph = ObjectGraph.create(new DaggerModule());
      }
      
      static getObjectGraph(){
          return sObjectGraph;
      }
  }
  </code></pre>
  <p>I am looking for a way to explicitly perform the injection <strong>after I call <code>setContentView</code></strong>.</p>
  <p>How do I go about doing this?</p>
  <hr />
  <h3>EDIT 1:</h3>
  <p>I got it to work like this - however I am not sure if this is the &quot;right way&quot;. What I do is</p>
  <ol>
  <li>Pass in the Activity to the <code>Module</code> constructor</li>
  <li>Ensure that I build the <code>ObjectGraph</code> <em>after</em> I do <code>setContentView()</code> so that a proper <code>Activity</code> instance is passed in to the <code>ObjectGraph</code>.</li>
  </ol>
  <p>My <code>Activity</code> is no longer a dagger <code>Module</code>.</p>
  <p>This translates to the following in code:</p>
  <pre><code>@Module(
      entryPoints = PresentationLayer.class
  )
  class DaggerModule{
  
      private Activity mActivity;
      
      public DaggerModule (Activity activity){
          this.mActivity = activity;
      }
      
      @Provides PresentationLayer providePresentation(){
          return new PresentationLayer(mActivity);
      }
  }
  </code></pre>
  <p>And this is how my <code>Activity</code> looks:</p>
  <pre><code>class MainActivity extends Activity{
      
      @Override
      public void onCreate(Bundle bundle){
          super.onCreate(bundle);
          setContentView(R.layout.main_activity);
          ObjectGraph objectGraph = CustomApplication.getObjectGraph(this);
          PresentationLayer pres = objectGraph.get(PresentationLayer.class);
          User user = //get the user here
          pres.presentData(user);
      }
  }
  </code></pre>
  <p>This solution seems to address the points raised by Jesse Wilson in <a href="https://stackoverflow.com/a/13992119/570930">this answer</a>. However, I am concerned about the performance, since the <code>ObjectGraph</code> is going to be built <strong>every time the <code>Activity</code> is created</strong>.</p>
  <p>Any thoughts?</p>