So im trying to set an observer and update fragment views depending on the response. The thing is when i first enter the fragment all works good, but if go back to other fragment, and re-enter the fragment it triggers the observe, and even executes the code, but doesn't change nothing on view.
override fun onStart() {
bindObservers()
loadUI()
super.onStart()
}
private fun loadUI() {
/**
* Load Variables
*
* Note:
* This Should be loaded as soon as possible in loadUI
*
* */
goalsViewModel.getFitnessModel()
/**
* General
* */
}
private fun bindObservers() {
/**
* Ideal Weight
*/
goalsViewModel.getFitnessModelLiveData.observe(viewLifecycleOwner) { networkResultEvent ->
networkResultEvent.getContentIfNotHandled()?.let {
when (it) {
is NetworkResult.Success -> {
fitnessReport = it.data!!
binding.minWeightRecommendedValue.text = fitnessReport.idealWeight.lowerLimit.toString() + " Kg"
binding.maxWeightRecommendedValue.text = fitnessReport.idealWeight.upperLimit.toString() + " Kg"
binding.maintainWeightValue.text = "${fitnessReport.maintain.calories.toString()} Calories"
binding.gain05Value.text = fitnessReport.plusHalf.calories.toString() + " Calories"
binding.gain1Value.text = fitnessReport.plus.calories.toString() + " Calories"
binding.lose05Value.text = fitnessReport.minusHalf.calories.toString() + " Calories"
if (fitnessReport.minus.calories == null)
binding.lose1.isEnabled = false
else
binding.lose1Value.text = fitnessReport.minus.calories.toString() + " Calories"
teste()
}
is NetworkResult.Error -> {
// Handle error if needed
}
is NetworkResult.Loading -> {
// Handle loading state if needed
}
}
}
}
}
private fun teste(){
binding.fragmentGoalCreateGoalWeightPb.visibility = View.GONE
binding.fragmentGoalCreateGoalWeightParentCl.visibility = View.VISIBLE
}
I've tried to put breakpoint on teste() and it does execute the code in it.
I also tried to:
override fun onStart() {
bindObservers()
loadUI()
teste()
super.onStart()
}
and it does change the View, so i think its the observer but i dont why or how to fix it.