HomeFragment Everything seems to be done correctly, but when you flip the screen, the resulting value from the EditText disappears from the TextView. What to do tell me pls HomeViewModel MainActivity
CodePudding user response:
what you are doing wrong is that you start observing the value from the viewmodel only after you click btnSend
.
This creates two problems. The first is the obvious one you have found already. You lose the value upon rotating because you have a new View.
The second one is that every time you click on btnSend you are re-observing your LiveData with a new observer object.
You have to move the folowing code inside your OnCreateView
after setting the binding
value
viewModel.myValue.observe(viewLifecycleOwner) {
binding.txtResult.text = viewModel.myValue.value
}
You can also replace the second part of the assignment with just it
as it is suggested in the answer from @JustSightseeing
CodePudding user response:
Try replacing:
viewModel.myValue.observe(viewLifecycleOwner) {
binding.txtResult.text = viewModel.myValue.value
}
with
viewModel.myValue.observe(viewLifecycleOwner) {
binding.txtResult.text = it
}