title: Activity-Fragment Communication w/ Hilt
body: <p>I have an app with a single activity but with many Fragments. I am using <code>ViewModel</code> for my Activity-Fragment communication. Lately, I am using Hilt, and I am having a problem now communicating between my activity and fragments.</p>
  <p>My Viewmodel</p>
  
  <pre class="lang-kotlin prettyprint-override"><code>@HiltViewModel
  class AppViewModel @Inject internal constructor(
  ): ViewModel() {
  
      private var _data = MutableLiveData&lt;String&gt;()
      val data: LiveData&lt;String&gt;
          get() = _data
      fun insertData(dataStr: String) {
          _data.value = dataStr
      }
  }
  </code></pre>
  <p>My MainActivity</p>
  <pre class="lang-kotlin prettyprint-override"><code>@AndroidEntryPoint
  class MainActivity : AppCompatActivity() {
  
      private val mViewModel: AppViewModel by viewModels()
      private var dataString: String? = null
  
      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
  
          mViewModel.data.observe(this, {
              dataString = it
          })
      }
  }
  </code></pre>
  <p>One of my Fragments</p>
  <pre class="lang-kotlin prettyprint-override"><code>@AndroidEntryPoint
  class ReportFragment : Fragment() {
  
      private val reportViewModel: ReportViewModel by viewModels()
      private val appViewModel: AppViewModel by viewModels()
  
      override fun onCreateView(
          inflater: LayoutInflater,
          container: ViewGroup?,
          savedInstanceState: Bundle?,
      ): View { 
          ...
          appViewModel.insertData(&quot;Hello&quot;)
          ...
      }
  }
  </code></pre>
  <p>When I run the app, I am getting <code>null</code> as a result of <code>data</code>. Any solution to solve this?</p>