title: How to reuse a Fragment and ViewModel with different Repository Implementation injected by Dagger2.2
body: <p>I'm kinda new to Android development and I have been stuck finding a way to do this pattern using some android libraries like Dagger2, Fragments, and ViewModel.</p>
  <p>I hope some of you can help me with this or stating what's the common way to do this on android.</p>
  <p><strong>I'm looking for something like this:</strong></p>
  <pre><code>class FragmentPager: Fragment() {
  
  @Inject
  @Named(&quot;FullListFragment&quot;)
  lateinit var listFragment: ListFragment
  
  @Inject
  @Named(&quot;FilteredListFragment&quot;)
  lateinit var filteredListFragment: ListFragment
  
  //Use fragments in the viewPager. 
  
  }
  
  </code></pre>
  <h1>What I am trying to do:</h1>
  <p>I have a Fragment that shows a list of elements. It also has a ViewModel responsible for updating the list among other things. The ViewModel gets the list from a Repository that queries a database. Pretty straightforward so far.</p>
  <p>My use case is that my app shows the list of elements in a lot of different areas within the application but with different data. For example full list, filtered list...</p>
  <p>My idea was to create the repository as an interface with a single method: <code>fun getItems(): List&lt;Item&gt;</code> and different instances for each data source. As a result, I have:</p>
  <ul>
  <li>ListFragment (class inheriting from Fragment)</li>
  <li>ListViewModel (class inheriting from ViewModel)</li>
  <li>ListRepository (interface)</li>
  <li>FullListRepository (class implementing ListRepository) --&gt; Gets all items from DB</li>
  <li>FilterListRepository (class implementing ListRepository) --&gt; Gets filter items from DB</li>
  <li>JoinedListRepository (class implementing ListRepository) --&gt; Gets items from DB but from joining different tables</li>
  </ul>
  <p>These elements will work together in an ideal world like this:</p>
  <pre><code>fun buildListFragment(repository: ListRepository) {
      val viewModel = ListViewModel(repository)
      val fragment = ListFragment(viewModel)
      return fragment
  }
  
  val fullListFragment = buildListFragment(FullListRepository())
  val filteredListFragment = buildListFragment(FilterListRepository())
  val joinedListFragment = buildListFragment(JoinedListRepository())
  </code></pre>
  <p><strong>How can I do something like these using Dagger2 to inject dependencies, ViewModelFactory to create ViewModels, and Fragments.</strong></p>
  <h2>Limitations I encounter:</h2>
  <ul>
  <li>My ViewModel has params so only can be created via ViewModelFactory.</li>
  <li>ViewModel cannot be injected by the constructor to the Fragment and need to be created inside using viewModelFactory in onCreate. At this point is it impossible to tell Dagger2 which ListRepository implementation should use to create ViewModelFactory.</li>
  </ul>
  <pre><code>class ListFragment: Fragment() {
     @Inject
     lateinit var viewModelFactory: ViewModelProvider.Factory
  
     override fun onCreateView(
          inflater: LayoutInflater,
          container: ViewGroup?,
          savedInstanceState: Bundle?
      ): View {
  
          viewModel = ViewModelProvider(this, viewModelFactory).get(CardListViewModel::class.java)
      }
  
  }
  </code></pre>
  <ul>
  <li>I know that you can use @Named annotation to request injection of the different repository implementations but <strong>when the factory is created inside Fragment body it is too late to request the @Named repository cause you know which repository you need before creating the fragment not after.</strong></li>
  </ul>
  <h2>Questions:</h2>
  <ul>
  <li><strong>Is this solution possible using Dagger2 and ViewModels?</strong></li>
  <li><strong>How Can I achieve it? Any advice?</strong></li>
  <li><strong>Do you usually use another pattern that could fit in the same use case?</strong></li>
  </ul>