title: Using Activity ViewModel in Fragment's Layout with Databinding
body: <p>Trying to access an activities viewModel in a fragment's layout using data binding. I am not sure if I am just doing something wrong, or if it's something that just doesn't work.</p>
  
  <p>The MainViewModel has a MutableLiveData property called _dateString, and a function called getDateString() that returns the LiveData of that property.</p>
  
  <pre><code>private val _dateString = MutableLiveData&lt;String&gt;()
  fun getDateString(): LiveData&lt;String&gt; {
      return _dateString
  }
  </code></pre>
  
  <p>That function is then called in the Calendar fragment's layout. I have both the mainModel and the viewModel in the data section of the layout. ( I stripped out most of the layout so its easier to read )</p>
  
  <pre><code>&lt;layout
      xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"&gt;
  &lt;data&gt;
      &lt;import type="android.view.View" /&gt;
      &lt;variable
              name="mainModel"
              type="{path}.features.main.MainViewModel" /&gt;
      &lt;variable
              name="viewModel"
              type="{path}.features.main.calendar.CalendarViewModel" /&gt;
  &lt;/data&gt;
  
  &lt;androidx.constraintlayout.widget.ConstraintLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:animateLayoutChanges="true"&gt;
  
      &lt;Button
              android:id="@+id/header_title"
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              style="@style/Widget.MaterialComponents.Button"
              android:text="@{mainModel.getDateString()}"/&gt;
  
  &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  </code></pre>
  
  <p></p>
  
  <p>In the fragment itself, I then bind the variables as such</p>
  
  <pre><code>binding = FragCalendarBinding.inflate(inflater, container, false).apply {
          viewModel = model
          mainModel = mainModel
          listener = this@CalendarFragment
          lifecycleOwner = this@CalendarFragment
      }
  </code></pre>
  
  <p>For whatever reason, the layout shows the button without a value. Even if I update the _dateString, the binding doesn't display the changed string.</p>
  
  <p>If I move the same logic into the CalendarViewModel, it does show up and update. If I call the getDateString() in the Calendar's fragment code, it returns the value correctly.</p>
  
  <p>Are you just unable to have multiple view model's connected to a single layout? Or am I doing something wrong here.</p>