Help me out please.
I want to observe some String using LiveData
.
OnChanged()
getting triggered once upon app start, but when Im changing the value of string1
by button click, onChange()
does not trigger and information is not updated. TextView
keeps showing "Wow"
I do everything exactly as described here.
The ViewModel
:
class CurrentViewModel : ViewModel() {
val currentName: MutableLiveData<String> by lazy {
MutableLiveData<String>()
}
}
The Fragment:
class CurrentFragment : Fragment(R.layout.current_fragment) {
private val viewModel: CurrentViewModel by viewModels()
var string1 = "Wow!"
override fun onActivityCreated(savedInstanceState: Bundle?)
val nameObserver = Observer<String> { newName ->
textview.text = newName }
viewModel.currentName.value = string1
viewModel.currentName.observe(activity!!, nameObserver)
button.setOnClickListener {
string1 = "some new string"
}
}