title: How to check whether email or mobile is registered or not in Firebase
body: <p>I want to implement sign up with two activities with the help of firebase, first sign up activity contains email mobile and password, and in this activity i want to check whether the entered email ID or mobile is registered or not, if it does not then move the data(i.e. email, mobile no. and password) to next activity where final registration will happen. The two methods which are present in the code i.e. <strong>userMobileExists() and userEmailExists()</strong> to check the email and mobile. But the problem is these are asynchronous so when I go to next activity then the Toast appear that email is already registered.
  I am returning valid, if all the valid are true then it goes to next activity , I debugged it and it returns valid before going inside the method. It is because of asynchronous code, Please suggest something how it can be achieved on the first activity only. Or tell if I should provide the whole code.</p>
  
  <pre><code> public class SignupActivity extends AppCompatActivity {
      private static final String TAG = "SignupActivity";
      private static final int REQUEST_SIGNUP = 0;
  
      private Firebase mRef = new Firebase("https://abcdef.firebaseio.com/");
      private User user;
      private FirebaseAuth mAuth;
      private FirebaseAuth.AuthStateListener mAuthListener;
      private ProgressDialog mProgressDialog;
      boolean valid = true;
      String email;
      String mobile;
      String password;
  
      @Bind(R.id.input_email)
      EditText _emailText;
      @Bind(R.id.input_mobile)
      EditText _mobileText;
      @Bind(R.id.input_password)
      EditText _passwordText;
      @Bind(R.id.btn_next)
      Button _signupButton;
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          setContentView(R.layout.activity_signup);
          ButterKnife.bind(this);
  
          //For Full screen
          getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
  
          mAuth = FirebaseAuth.getInstance();
  
          //Back button initialization
          Button backButton = (Button) findViewById(R.id.back_signup);
          backButton.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  Intent in = new Intent(view.getContext(), LoginActivity.class);
                  startActivity(in);
              }
          });
  
          mAuth = FirebaseAuth.getInstance();
          // mRef = Firebase(Config.FIREBASE_URL);
  
  
          _signupButton.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  signup();
              }
          });
  
      }
  
      protected void setUpUser() {
  
          user.setPhoneNumber(_mobileText.getText().toString());
          user.setEmail(_emailText.getText().toString());
          user.setPassword(_passwordText.getText().toString());
      }
  
      //private void
  
      public void signup() {
          Log.d(TAG, "Signup");
          //showProgressDialog();
          if (validate() &amp;&amp; userEmailExist() &amp;&amp; userMobileExist()) {
              onSignupSuccess();
  
          } else {
              onSignupFailed();
              return;
          }
  
          //_signupButton.setEnabled(false);
  
          email = _emailText.getText().toString();
          mobile = _mobileText.getText().toString();
          password = _passwordText.getText().toString();
  
  
          // TODO: Implement your own signup logic here.
  
  
      }
  
  
      public void onSignupSuccess() {
          //_signupButton.setEnabled(true);
          Log.d(TAG, "NEXT        BUTTTON");
          //hideProgressDialog();
          Intent in = new Intent(this, SignupActivity2.class);
          in.putExtra("Email", _emailText.getText().toString());
          in.putExtra("Mobile", _mobileText.getText().toString());
          startActivity(in);
  
          /*setResult(RESULT_OK, null);
          finish();*/
      }
  
      public void onSignupFailed() {
  
          // hideProgressDialog();
          Toast.makeText(getBaseContext(), "SignUp failed", Toast.LENGTH_LONG).show();
  
          // _signupButton.setEnabled(true);
      }
  
      public boolean validate() {
  
  
          email = _emailText.getText().toString();
          mobile = _mobileText.getText().toString();
          password = _passwordText.getText().toString();
  
          if (email.isEmpty() || !android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
              _emailText.setError("enter a valid email address");
              valid = false;
          } else {
              _emailText.setError(null);
          }
  
          if (mobile.isEmpty() || mobile.length() != 10) {
              _mobileText.setError("Enter Valid Mobile Number");
              valid = false;
          } else {
              _mobileText.setError(null);
          }
  
          if (password.isEmpty() || password.length() &lt; 4 || password.length() &gt; 10) {
              _passwordText.setError("between 4 and 10 alphanumeric characters");
              valid = false;
          } else {
              _passwordText.setError(null);
          }
          return valid;
      }
  
      public boolean userEmailExist() {
          //private Firebase mRef = new Firebase("https://.firebaseio.com/users/");
          mRef.child("users").orderByChild("email").addListenerForSingleValueEvent(new ValueEventListener() {
              @Override
              public void onDataChange(DataSnapshot dataSnapshot) {
                  if (dataSnapshot.getValue()== _emailText.getText().toString()) {
  
                      Toast.makeText(getBaseContext(), "Email already exist. Please choose a different one", Toast.LENGTH_SHORT).show();
                      _emailText.setError("Email already exist. Please choose a different one");
                      valid = false;
                  } else {
                      email = _emailText.getText().toString();
                  }
              }
  
              @Override
              public void onCancelled(FirebaseError firebaseError) {
  
              }
          });
          return valid;
      }
  
      public boolean userMobileExist() {
          mRef.child("users").
                  orderByChild("cellPhone").equalTo(_mobileText.getText().toString()).addListenerForSingleValueEvent(new ValueEventListener() {
              @Override
              public void onDataChange(DataSnapshot dataSnapshot) {
                  if (dataSnapshot.exists()) {
                      _mobileText.setError("Mobile Number already exist");
                      valid = false;
                  } else {
                      mobile = _mobileText.getText().toString();
  
                  }
              }
  
              @Override
              public void onCancelled(FirebaseError firebaseError) {
  
              }
          });
  
  
          return valid;
      }
  
  
      @Override
      public void onBackPressed() {
          // Disable going back to the MainActivity
          moveTaskToBack(true);
      }
  
      public void showProgressDialog(){
          if(mProgressDialog == null){
              mProgressDialog = new ProgressDialog(this);
              mProgressDialog.setMessage("loading");
              mProgressDialog.setIndeterminate(true);
          }
          mProgressDialog.show();
      }
  
      public void hideProgressDialog(){
  
          if(mProgressDialog != null &amp;&amp; mProgressDialog.isShowing()){
              mProgressDialog.dismiss();
          }
      }
  }
  </code></pre>