Issue
I am using MVVM architecture and having a hard time figuring out where to store value observed from LiveData which just needs to be used in if statement in onActivityResult()
of the same Activity
or any other method.
Suppose, I've this method that observes live data from the viewModel
in the Activity
.
fun subscribeObservers() {
myViewModel.liveData.observe(viewLifeCycleOwner, Observer { myLiveDataString ->
setTextField(myLiveDataString)
});
}
And I want to use that particular value observed from LiveData in onActivityResult
of the same Activity
.
Suppose like this,
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (requestCode == RC) {
if (myLiveDataString == "ERROR") {
showErrorDialogToUser()
}
}
}
So my question is where should I store the data observed from LiveData to check for If
statement? Should I just store it in an Activity
? Is it a good idea?
Solution
Just use myViewModel.liveData.value
to get the value.
Shouldn't liveData value be retrieved only through observer method?
Not only. When observing, you get notified when the value changes. Nothing prevents you from checking the value. LiveData is just a data holder.
Answered By - Onik
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.