Activity unable to observe second livedata object within viewmodel

38 Views Asked by At

I have two livedata objects within my viewmodel. The activity it able to observe the first livedata object but second object is not being observed . I can see the viewmodel functions which set the livedata objects are bing logged correctly but nothing is being registered for the second livedata2 object

class ViewModel {
 
private val _livedata1 = MutableLiveData<String>()
  val livedata1: LiveData<String> = _livedata1
 private val _livedata2 = MutableLivleData<String>()
  val livedata2: LiveData<String> = _livedata2

 fun callLiveData1(response: String) {
    // logger.d(“being called")
    _liveData1.value = response
  }

  fun callLiveData2(response: String) {
    // logger.d(“being called")
    _liveData2.value = response
  }

}

class Activity : AppCompatActivity(){
 private val viewModel by lazy { ViewModelProvider(this).get(ViewModel::class.java) }

 override fun onCreate(savedInstanceState: Bundle?) {

   viewModel.livedata1.observe(this) { response: String ->
     // logger.d(“being called")
    }

    viewModel.livedata2.observe(this) { response: String ->
     // logger.d(“NOT being called")
    }

 }
}
0

There are 0 best solutions below