title: Android [Kotlin] - Inserting a new item into a selected Child-List on a MasterDetail-Flow using shared viewModel
body: <p>I am quite new to android and i am trying to follow the recommended app architecture.</p>
  
  <p>The app basically consists of two entities using a Room-Database: Training and Exercises. They have a many-to-many relationship, realized as proposed in the android developer guide with an extra reference entity and a POJO:</p>
  
  <pre><code>@Entity(
      tableName = "training_exercise_ref",
      primaryKeys = ["trainingId", "exerciseId"])
  data class TrainingExerciseCrossRef (
      var trainingId: Long,
      var exerciseId: Long
  )
  
  data class TrainingWithExercise (
      @Embedded val training: Training,
  
      @Nullable
      @Relation(
          parentColumn = "trainingId",
          entityColumn = "exerciseId",
          associateBy = Junction(TrainingExerciseCrossRef::class)
      )
      val exercises: List&lt;Exercise&gt;?
  </code></pre>
  
  <p>A training doesnt need exercises by default.</p>
  
  <p>Now i want to list all trainings in a first Master-View (recyclerview) and if clicked on a training, i want to show the corresponding exercises in a separate Detail/Exercise-Recyclerview.
  I did this by two different fragments, and i am sharing the data via a ViewModel as proposed in the developer guide:</p>
  
  <pre><code>class DashboardViewModel(application: Application) : AndroidViewModel(application) {
  
      private val trainingRepository: TrainingRepository
  
      val allTrainingWithExercises: LiveData&lt;List&lt;TrainingWithExercise&gt;&gt;
      val selectedTraining = MutableLiveData&lt;TrainingWithExercise&gt;()
  
      init {
          val trainingDao = TrainingRoomDatabase.getDatabase(application, viewModelScope).trainingDao()
          trainingRepository = TrainingRepository(trainingDao)
  
          allTrainingWithExercises = trainingRepository.allTrainingWithExerciseEntries
      }
  
      fun selectTraining(trainingWithExercise: TrainingWithExercise) {
          selectedTraining.value = trainingWithExercise
      }
  
      fun insertExerciseToTraining(trainingId: Long, exercise: Exercise) = viewModelScope.launch {
          val newExerciseId: Long = exerciseRepository.insert(exercise)
          trainingRepository.insertExerciseOfTraining(trainingId, newExerciseId)
      }
  </code></pre>
  
  <p>So, when i click on a training, i save the selection in the viewModel, tried to set an observer in the fragment</p>
  
  <pre><code>dashboardViewModel.selectedTraining.observe(viewLifecycleOwner, Observer { exercises -&gt;
              exercises?.let { adapter.setTraining(it) }
          })
  </code></pre>
  
  <p>and display the selected training with the exercise list in the detail-fragment.
  In the detail fragment i also want to be able to insert new exercises. </p>
  
  <p><strong>Here comes my problem:</strong><br>
  When i am adding a new exercise, i use the "insertExerciseToTraining" function above and then in the repository, i am inserting the exercise and a new trainingExerciseCrossRef.
  But in the detail fragment, the exercise list doesnt get updated. Only when i move back to the training overview and select the training again.
  Is this even the proper way of doing this, using a shared view model?<br>
  As a fast fix, i only share the training id in the viewModel and query the trainingWithExercise from the DB again, observing and displaying it. Then, on inserting new exercises, the list gets updated correctly. But that doesnt seem to be the best way achieving my goal or the best way using viewModels.<br>
  Any suggestions?</p>