title: My ViewModel does not survive configuration changes
body: <p>I'm trying out Dagger 2 with MVVM. Here is what I have so far,</p>
  
  <p>My ViewModel binding module:</p>
  
  <pre><code>@Module
  public abstract class ViewModelModule {
  
      @Binds
      @IntoMap
      @ViewModelKey(MedicationsViewModel.class)
      @Singleton
      abstract ViewModel bindMedicationsViewModel(MedicationsViewModel viewModel);
  
      @Binds
      @Singleton
      abstract ViewModelProvider.Factory bindViewModelFactory(ViewModelFactory factory);
  }
  </code></pre>
  
  <p>My Fragment:</p>
  
  <pre><code>class MedicationsFragment : DaggerFragment() {
      private lateinit var binding : FragmentMedicationsBinding
      private lateinit var viewModel: MedicationsViewModel
  
      @Inject lateinit var viewModelFactory : ViewModelFactory
  
      override fun onCreateView(
              inflater: LayoutInflater,
              container: ViewGroup?,
              savedInstanceState: Bundle?
      ): View? {
          binding = FragmentMedicationsBinding.inflate(inflater, container, false)
  
          viewModel = ViewModelProviders.of(this, viewModelFactory).get(MedicationsViewModel::class.java)
  
          val adapter = MedicationAdapter()
          binding.rvMedications.adapter = adapter
  
          viewModel.getMedications().observe(viewLifecycleOwner, Observer { items -&gt;
              if (items != null) {adapter.submitList(items); adapter.notifyDataSetChanged()}
          })
  
          return binding.root
      }
  }
  </code></pre>
  
  <p>And my ViewModel is like this:</p>
  
  <pre><code>class MedicationsViewModel @Inject constructor(
      private val repository: MedicationRepository,
      ): ViewModel() {
          private var medications : MutableLiveData&lt;MutableList&lt;Medication&gt;&gt; = MutableLiveData()
          private val disposable: CompositeDisposable = CompositeDisposable()
  
          fun getMedications() : MutableLiveData&lt;MutableList&lt;Medication&gt;&gt;{
              getMockMedications()
              return medications }
  
          private fun getMockMedications(){
              val medication1 = Medication(1,"Mock Med 1", "2018-01-01","once per day",true,null)
              val medication2 = Medication(2,"Mock Med 2", "2018-01-02","once per day",false,"before 15 minutes")
  
              val mockList: MutableList&lt;Medication&gt; = mutableListOf()
              mockList.add(medication1)
              mockList.add(medication2)
  
              medications.postValue(mockList)
          }
  
          fun addMedication(medication: Medication){
              medications.value?.add(medication)
              medications.notifyObserver()
          }
  
          fun &lt;T&gt; MutableLiveData&lt;T&gt;.notifyObserver() {
              this.postValue(this.value)
          }
      }
  </code></pre>
  
  <p>Through a button in my fragment, I am opening an activity and adding another Medication item to my viewmodel. After finish() out from my activity, I can see the newly added Medication item in my RecyclerView in fragment. However, when I rotate the screen I lost the newly added item and left with my mock Medication items again. This is where I think a new instance of my viewmodel is being created. </p>
  
  <p>The problem might be a cause of Dagger creating a new instance of my viewmodel everytime my fragment (and container activity) is recreated. But I thought annotating my ViewModelFactory with @Singleton scope would have fixed it. </p>
  
  <p>Can anyone see what I am missing?</p>
  
  <p>Thanks.</p>