There is a project written by another programmer. And in it there is such a code
class CardsViewModel : ViewModel() {
val selected = MutableLiveData<PaymentCard>()
companion object {
@JvmStatic
var keyId = 0
}
}
Every time some data is added to the ViewModel, this keyId is incremented:
val cardsViewModel = ViewModelProviders.of(requireActivity())
.get(CardsViewModel.keyId.toString(), CardsViewModel::class.java)
CardsViewModel.keyId++
cardsViewModel.selected.value = PaymentCard.EMPTY
Then gets it this way:
val cardsViewModel = ViewModelProviders.of(requireActivity()).get(CardsViewModel.keyId.toString(), CardsViewModel::class.java)
bindingId = cardsViewModel.selected.value?.bindingId ?: ""
I just recently started learning LiveData and it’s unclear to me what this id gives and how to work with it? I got the impression that I put the data tied to some key and then retrieve it using this key =) But this causes a lot of questions.
P.S. The project was written poorly, so maybe this is some kind of crutch, but due to lack of experience with LiveData, I cannot understand this