First of all, I am new in LiveData. I found this from the android official website.
An exception to this behavior is that observers also receive an update when they change from an inactive to an active state. Furthermore, if the observer changes from inactive to active a second time, it only receives an update if the value has changed since the last time it became active.
I want to know how to detect the state.
public void doLogin(View view){
final LiveData<Pojo> liveData = loginViewModel.checkLoginAndgetUserData(email.getText().toString(),password.getText().toString());
liveData.observe(LoginActivity.this, new Observer<Pojo>() {
@Override
public void onChanged(@Nullable Pojo pojo) {
Boolean isValid = loginViewModel.isValid(pojo);
if(isValid){
Toast.makeText(LoginActivity.this, "Success", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(LoginActivity.this, "Failed", Toast.LENGTH_SHORT).show();
}
}
});
}
Here is my code. If Users first time failed to login and try to login again, that time onChanged
method call without data changed. That time toast appears again. After sometime When network call completed then again onChanged
method call.