title: LiveData not able to observe the changes
body: <p>I am updating a <strong>LiveData value from a DialogFragment in the ViewModel</strong>, but not able to get the value in Fragment.</p>
  
  <p><strong>The ViewModel:</strong></p>
  
  <pre><code>class OtpViewModel(private val otpUseCase: OtpUseCase, analyticsModel: IAnalyticsModel) : BaseViewModel(analyticsModel) {
      override val globalNavModel = GlobalNavModel(titleId = R.string.otp_contact_title, hasGlobalNavBar = false)
  
      private val _contactListLiveData = MutableLiveData&lt;List&lt;Contact&gt;&gt;()
      val contactListLiveData: LiveData&lt;List&lt;Contact&gt;&gt;
          get() = _contactListLiveData
  
      private lateinit var cachedContactList: LiveData&lt;List&lt;Contact&gt;&gt;
      private val contactListObserver = Observer&lt;List&lt;Contact&gt;&gt; {
          _contactListLiveData.value = it
      }
  
  
  
      private lateinit var cachedResendOtpResponse: LiveData&lt;LogonModel&gt;
      private val resendOTPResponseObserver = Observer&lt;LogonModel&gt; {
          _resendOTPResponse.value = it
      }
  
      private var _resendOTPResponse = MutableLiveData&lt;LogonModel&gt;()
      val resendOTPResponseLiveData: LiveData&lt;LogonModel&gt;
          get() = _resendOTPResponse
  
      var userSelectedIndex : Int = 0 //First otp contact selected by default
  
      val selectedContact : LiveData&lt;Contact&gt;
          get() = MutableLiveData(contactListLiveData.value?.get(userSelectedIndex))
  
      override fun onCleared() {
          if (::cachedContactList.isInitialized) {
              cachedContactList.removeObserver(contactListObserver)
          }
  
          if (::cachedOtpResponse.isInitialized) {
              cachedOtpResponse.removeObserver(otpResponseObserver)
          }
  
          super.onCleared()
      }
  
      fun updateIndex(pos: Int){
          userSelectedIndex = pos
      }
  
      fun onChangeDeliveryMethod() {
          navigate(
              OtpVerificationHelpCodeSentBottomSheetFragmentDirections
                  .actionOtpContactVerificationBottomSheetToOtpChooseContactFragment()
          )
      }
  
      fun onClickContactCancel() {
          navigateBackTo(R.id.logonFragment, true)
      }
  
      fun retrieveContactList() {
          cachedContactList = otpUseCase.fetchContactList()
          cachedContactList.observeForever(contactListObserver)
      }
  
  
  
      fun resendOTP(contactId : String){
          navigateBack()
          cachedResendOtpResponse = otpUseCase.resendOTP(contactId)
          cachedResendOtpResponse.observeForever(resendOTPResponseObserver)
  
      }
  }
  </code></pre>
  
  <p><strong>The BaseViewModel:</strong></p>
  
  <pre><code>abstract class BaseViewModel(val analyticsModel: IAnalyticsModel) : ViewModel() {
      protected val _navigationCommands: SingleLiveEvent&lt;NavigationCommand&gt; = SingleLiveEvent()
      val navigationCommands: LiveData&lt;NavigationCommand&gt; = _navigationCommands
  
      abstract val globalNavModel: GlobalNavModel
  
  
      /**
       * Posts a navigation event to the navigationsCommands LiveData observable for retrieval by the view
       */
      fun navigate(directions: NavDirections) {
          _navigationCommands.postValue(NavigationCommand.ToDirections(directions))
      }
  
      fun navigate(destinationId: Int) {
          _navigationCommands.postValue(NavigationCommand.ToDestinationId(destinationId))
      }
  
      fun navigateBack() {
          _navigationCommands.postValue(NavigationCommand.Back)
      }
  
      fun navigateBackTo(destinationId: Int, isInclusive: Boolean) {
          _navigationCommands.postValue(NavigationCommand.BackTo(destinationId, isInclusive))
      }
  
      open fun init() {
          // DEFAULT IMPLEMENTATION - override to initialize your view model
      }
  
  
      /**
       * Called from base fragment when the view has been created.
       */
      fun onViewCreated() {
          analyticsModel.onNewState(getAnalyticsPathCrumb())
      }
  
      /**
       * gets the Path for the current page to be used for the trackstate call
       *
       * Override this method if you need to modify the path
       *
       * the page id for the track state call will be calculated in the following manner
       * 1) analyticsPageId
       * 2) titleId
       * 3) the page title string
       */
      protected fun getAnalyticsPathCrumb() : AnalyticsBreadCrumb {
  
          return analyticsBreadCrumb {
              pathElements {
                  if (globalNavModel.analyticsPageId != null) {
                      waPath {
                          path = PathElement(globalNavModel.analyticsPageId as Int)
                      }
                  } else if (globalNavModel.titleId != null) {
                      waPath {
                          path = PathElement(globalNavModel.titleId as Int)
                      }
                  } else {
                      waPath {
                          path = PathElement(globalNavModel.title ?: "")
                      }
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p><strong>The DialogFragment:</strong></p>
  
  <pre class="lang-kotlin prettyprint-override"><code>class OtpVerificationHelpCodeSentBottomSheetFragment : BaseBottomSheetDialogFragment(){
  
      private lateinit var rootView: View
      lateinit var binding: BottomSheetFragmentOtpVerificationHelpCodeSentBinding
  
      override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
  
          viewModel = getViewModel&lt;OtpViewModel&gt;()
  
          binding = DataBindingUtil.inflate(inflater, R.layout.bottom_sheet_fragment_otp_verification_help_code_sent, container, false)
  
          rootView = binding.root
  
          return rootView
      }
  
      override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
          super.onViewCreated(view, savedInstanceState)
  
  
          val otpViewModel = (viewModel as OtpViewModel)
          binding.viewmodel = otpViewModel
  
          otpViewModel.resendOTPResponseLiveData.observe(viewLifecycleOwner, Observer {
  
              it?.let { resendOtpResponse -&gt;
                  if(resendOtpResponse.statusCode.equals("000")){
                      //valid status code
                      requireActivity().toastMessageOtp(getString(R.string.otp_code_verification_sent))
                  }else{
                      //show the error model
                      //it?.errorModel?.let { it1 -&gt; handleDiasNetworkError(it1) }
                  }
              }
  
          })
      }
  }
  </code></pre>
  
  <p>I am calling the <strong>resendOTP(contactId : String)</strong> method of the viewmodel from the xml file of the <strong>DialogFragment</strong>:</p>
  
  <pre><code> &lt;TextView
              android:id="@+id/verification_help_code_sent_resend_code"
              style="@style/TruTextView.SubText2.BottomActions"
              android:layout_height="@dimen/spaceXl"
              android:gravity="center_vertical"
              android:text="@string/verification_help_resend_code"
              android:onClick="@{() -&gt; viewmodel.resendOTP(Integer.toString(viewmodel.userSelectedIndex))}"
              app:layout_constraintStart_toStartOf="parent"
              app:layout_constraintTop_toBottomOf="@+id/top_guideline" /&gt;
  </code></pre>
  
  <p><strong>Now whenever I try to call resendOTPResponseLiveData from the Fragment  it does not gets called:</strong></p>
  
  <pre><code>override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
          super.onViewCreated(view, savedInstanceState)
          Log.d("OtpVerify" , "OnViewCreatedCalled")
          viewModel.onViewCreated()
          val otpViewModel = (viewModel as OtpViewModel)
  
          binding.lifecycleOwner = this
          binding.viewmodel = otpViewModel
          binding.toAuthenticated = OtpVerifyFragmentDirections.actionOtpVerifyFragmentToAuthenticatedActivity()
          binding.toVerificationBtmSheet = OtpVerifyFragmentDirections.actionOtpVerifyFragmentToOtpContactVerificationCodeSentBottomSheet()
  
  
          otpViewModel.resendOTPResponseLiveData.observe(viewLifecycleOwner, Observer {
              if(it?.statusCode.equals("000")){
                  //valid status code
                  requireActivity().toastMessageOtp(getString(R.string.otp_code_verification_sent))
              }else{
                  //show the error model
                  it?.errorModel?.let { it1 -&gt; handleDiasNetworkError(it1) }
              }
          })
  
      }
  </code></pre>
  
  <p>So what wrong I am doing here.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Basically I need clicklistener(resend button click) in dialogfragment, and need to read it in the fragment. So I used the concept of SharedViewModel.</p>
  
  <p>So I make necessary changes in the ViewModel:</p>
  
  <pre><code>private val selected = MutableLiveData&lt;LogonModel&gt;()
  
   fun select(logonModel: LogonModel) {
          selected.value = logonModel
      }
  
      fun getSelected(): LiveData&lt;LogonModel&gt; {
          return selected
      }
  </code></pre>
  
  <p><strong>In the DialogFragment:</strong></p>
  
  <pre><code> otpViewModel.resendOTPResponseLiveData.observe(viewLifecycleOwner, Observer{
  
             otpViewModel.select(it);
  
          })
  </code></pre>
  
  <p>And in the fragment where I want to read the value:</p>
  
  <pre><code>otpViewModel.getSelected().observe(viewLifecycleOwner, Observer {
  
              Log.d("OtpVerify" , "ResendCalled")
              // Update the UI.
              if(it?.statusCode.equals("000")){
                  //valid status code
                  requireActivity().toastMessageOtp(getString(R.string.otp_code_verification_sent))
              }else{
                  //show the error model
                  it?.errorModel?.let { it1 -&gt; handleDiasNetworkError(it1) }
              }
          })
  </code></pre>
  
  <p>But it is still not working.</p>
  
  <p>Edit:</p>
  
  <p>ViewModel Source for fragment:</p>
  
  <pre><code>viewModel = getSharedViewModel&lt;OtpViewModel&gt;(from = {
              Navigation.findNavController(container as View).getViewModelStoreOwner(R.id.two_step_authentication_graph)
          })
  </code></pre>
  
  <p>ViewModel Source for dialogfragment:</p>
  
  <pre><code>viewModel = getViewModel&lt;OtpViewModel&gt;()
  </code></pre>