title: Architecture Components ViewModels inject with dagger with params that come from the Activity/Fragment
body: <p>I'm trying to add the new Architecture Components <code>ViewModel</code> to my application while injecting them with dagger. I based my code on what google showed <a href="https://github.com/googlesamples/android-architecture-components/tree/master/GithubBrowserSample/app/src/main/java/com/android/example/github/di" rel="nofollow noreferrer">here</a>. I'm trying to avoid having a <code>ViewModelFactory</code> for each <code>ViewModel</code> type, so I used the <code>ViewModelFactory</code> that receives <code>Map&lt;Class&lt;? extends ViewModel&gt;, Provider&lt;ViewModel&gt;&gt; creators</code>. It works for <code>ViewModels</code> that have dependencies with <code>@Singleton</code> scope. However, one of my <code>ViewModels</code> has a dependency that comes from the fragment. This is the module of that fragment:</p>
  
  <pre><code>@Module
  public abstract class DownloadIssueDialogFragmentModule {
  
      @Binds
      abstract DialogFragment dialogFragment(DownloadIssueDialogFragment dialogFragment);
  
      @Provides
      @FragmentScope
      static Issue provideIssue(DownloadIssueDialogFragment dialogFragment) {
          return dialogFragment.getIssue();
      }
  }
  </code></pre>
  
  <p>And my <code>ViewModelModule</code>:</p>
  
  <pre><code>@Module
  public abstract class ViewModelModule {
  
      @Binds
      abstract ViewModelProvider.Factory bindViewModelFactory(ViewModelFactory factory);
  
      @Binds
      @IntoMap
      @ViewModelKey(DownloadIssueViewModel.class)
      abstract ViewModel bindDownloadIssueViewModel(DownloadIssueViewModel viewModel);
  
  }
  </code></pre>
  
  <p>dagger says it can't provide <code>Issue</code>. It makes sense since <code>Map&lt;Class&lt;? extends ViewModel&gt;, Provider&lt;ViewModel&gt;&gt;</code> seems to be created at compile time. But I will only know the parameter during the scope of that fragment. How can I achieve this?</p>
  
  <p>Thank you.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>In the end I went with a different approach. Now I create a factory for each ViewModel and instead of injecting ViewModels, I inject the factory.</p>
  
  <p>I created this library: <a href="https://github.com/kakai248/AutoViewModelFactory" rel="nofollow noreferrer">AutoViewModelFactory</a></p>
  
  <p>To automatically generate the factories. It's the best solution I've found so far.</p>