Android live data - observe always fires after config change
I have used MutableSharedFlow instead of MutableLiveData, and solved the same problem as yours.
You can try this:
private val responseCode = MutableSharedFlow<Int>()
...
fun passChangeCallback() {
viewModelScope.launch {
responseCode.emit(serverResponse)
}
Because MutableSharedFlow don't replay a value that has already emited by defalut.
In addition to answer above, it's important to understand the scenarios in which using ViewModel & LiveData observers, to observe only once, this article explains them and shows a way to deal with it easily: Working with LiveData and Events
That is an intended behavior, as you can see in documents:
observe (LifecycleOwner owner, Observer observer) Adds the given observer to the observers list within the lifespan of the given owner. The events are dispatched on the main thread. If LiveData already has data set, it will be delivered to the observer.
If you want to observe the change in view state then you should create and observe a view state instead of a network request, google already provided an example for cases like this.