I\'m new to Android development and i am trying to understand Live Data with MVVM architecture. I am trying to make the main activity recognize when there is a change in an obje
here your problem is with repository code inside repository you are creating new object of mutable live data and observing different one.
Interface Callback{
onSuccess(String response)
onError(String error)
}
public void login(String username , String hashedPassword,Callback callback){
final MutableLiveData loggedInUser = new MutableLiveData<>();
User user = new User(username,hashedPassword);
usersRepositoryApi.login(user).enqueue(new Callback() {
@Override
public void onResponse(Call call, Response response) {
if (response.isSuccessful()) {
callback.onSuccess(response.body());
}
}
@Override
public void onFailure(Call call, Throwable t) {
callback.onError(null);
}
});
}
//login method of your viewmodel
public void login(String userName , String hashedPassword) {
usersRepository.login(userName, hashedPassword,new Callback(){
void onSuccess(String responsebody){
loggedInUser.setValue(responsebody);
}
void onError(String error){
loggedInUser.setValue(responsebody);
}
});
}