0

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.

Sunny Sultan
  • 1,090
  • 15
  • 20
  • a LiveData becomes inactive if it has no Active Observers. This means that if LifecycleOwner becomes `stopped`, then it is seen as `inactive`. So app being put to background makes the observers inactive. – EpicPandaForce Feb 24 '19 at 14:53
  • There was a lake of my knowledge about live data. Now I am clear than before. Thanks. – Sunny Sultan Feb 24 '19 at 19:25

0 Answers0