title: How do I inject dependencies into an Activity that require an argument from a previous Activity?
body: <p>I have a LoginActivity where the user logs in via Auth0 and returns an auth token. This token is passed to MainActivity:</p>
  
  <pre><code>Intent intent = new Intent(LoginActivity.this, MainActivity.class);
  intent.putExtra(KEY_ACCESS_TOKEN, credentials.getAccessToken());
  intent.putExtra(KEY_ID_TOKEN, credentials.getIdToken());
  startActivity(intent);
  </code></pre>
  
  <p>I was able to get dependency injection working with LoginActivity fine by <a href="https://github.com/codepath/android_guides/wiki/Dependency-Injection-with-Dagger-2" rel="nofollow noreferrer">following this guide</a>.</p>
  
  <p>Now I'm trying to inject dependencies into MainActivity. My MainActivity has a <code>MainActivityViewModel</code> to handle all the interactions between the UI and the data layer. I'd like to inject my API into my ViewModel:</p>
  
  <pre><code>PetshackApi apiService;
  
  @Inject
  public PetMapViewModel(PetshackApi apiService) {
      this.apiService = apiService;
  }
  </code></pre>
  
  <p>I have <code>ViewModelModule</code>, <code>ViewModelKey</code>, and <code>MainActivityViewModelFactory</code> (renamed from GithubViewModelFactory) defined. I injected the viewModelFactory at the top of my MainActivity:</p>
  
  <pre><code>@Inject
  ViewModelProvider.Factory viewModelFactory;
  </code></pre>
  
  <p>And then use the factory to get my viewModel:</p>
  
  <pre><code>viewModel = ViewModelProviders.of(this, viewModelFactory).get(MainActivityViewModel.class);
  </code></pre>
  
  <p>I set this up using <a href="https://stackoverflow.com/a/44506312/98933">this answer</a>.</p>
  
  <p>The problem is that my Retrofit/PetshackApi dependency will require the <code>accessToken</code> from the LoginActivity. So I defined another method in my MainActivity to allow retrieving it:</p>
  
  <pre><code>public String getAccessToken() {
      return getIntent().getStringExtra(LoginActivity.KEY_ACCESS_TOKEN);
  }
  </code></pre>
  
  <p>I'm having trouble setting up my modules/components/???. I think I need to inject MainActivity somehow into my modules so I tried following <a href="https://google.github.io/dagger/android" rel="nofollow noreferrer">Injecting Activity objects</a>.</p>
  
  <p>MainActivityComponent.java</p>
  
  <pre><code>@Component(modules={AndroidSupportInjectionModule.class, AppModule.class, MainActivityModule.class, ViewModelModule.class})
  public interface MainActivityComponent extends AndroidInjector&lt;PetApplication&gt; {
      @Component.Builder
      abstract class Builder extends AndroidInjector.Builder&lt;PetApplication&gt;{
          @BindsInstance
          abstract Builder application(Application application);
      }
      void inject(MainActivity mainActivity);
  }
  </code></pre>
  
  <p>MainActivityModule.java</p>
  
  <pre><code>@Module(subcomponents = MainActivitySubcomponent.class)
  abstract class MainActivityModule {
      @Binds
      @IntoMap
      @ActivityKey(MainActivity.class)
      abstract AndroidInjector.Factory&lt;? extends Activity&gt;
      bindMainActivityInjectorFactory(MainActivitySubcomponent.Builder builder);
  }
  </code></pre>
  
  <p>MainActivitySubcomponent.java</p>
  
  <pre><code>@Subcomponent(modules = MainActivityChildModule.class)
  public interface MainActivitySubcomponent extends AndroidInjector&lt;MainActivity&gt; {
      @Subcomponent.Builder
      public abstract class Builder extends AndroidInjector.Builder&lt;MainActivity&gt; {}
  }
  </code></pre>
  
  <p>MainActivityChildModule.java</p>
  
  <pre><code>@Module
  abstract class MainActivityChildModule {
      @Provides
      @Singleton
      Retrofit providesRetrofit(Application application, MainActivity mainActivity) {
          final String accessToken = mainActivity.getAccessToken();
          Interceptor interceptor = new Interceptor() {
              @Override
              public okhttp3.Response intercept(Chain chain) throws IOException {
                  Request newRequest = chain.request().newBuilder()
                          .addHeader("authorization", "Bearer " + accessToken).build();
                  return chain.proceed(newRequest);
              }
          };
  
          // Add the interceptor to OkHttpClient
          OkHttpClient.Builder builder = new OkHttpClient.Builder();
          builder.interceptors().add(interceptor);
          OkHttpClient client = builder.build();
  
          Retrofit retrofit = new Retrofit.Builder()
                  .baseUrl(application.getString(R.string.endpoint_url))
                  .addConverterFactory(GsonConverterFactory.create())
                  .client(client)
                  .build();
          return retrofit;
      }
  
      @Provides
      @Singleton // needs to be consistent with the component scope
      PetshackApi providesPetshackApiInterface(Retrofit retrofit) {
          return retrofit.create(PetshackApi.class);
      }
  }
  </code></pre>
  
  <p>Am I on the right track? Any hints or examples on how to do this?</p>