so yeah i made this app and im using custom registration instead of the firebase one, but i also need this code to work for other functions of my porject. i need the code to stop inserting the values once it finds they already exist on my firebase realtime database, my code checks that they exist but then continues on with adding them to the database.
private void checkDuplicates() {
final String username = UserName.getText().toString();
final String email = Email.getText().toString();
final String phone = MobileNumber.getText().toString();
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.child("userName").equals(username)) {
Toast.makeText(Registeration.this, "Entered username is unique ", Toast.LENGTH_SHORT).show();
checkDup = false;
} else {
Toast.makeText(Registeration.this, "Entered username is already registered", Toast.LENGTH_SHORT).show();
checkDup = true;
finish();
}
if (!dataSnapshot.child("email").equals(email)) {
Toast.makeText(Registeration.this, "Entered email is unique ", Toast.LENGTH_SHORT).show();
checkDup = false;
} else {
Toast.makeText(Registeration.this, "Entered email is already registered", Toast.LENGTH_SHORT).show();
checkDup = true;
finish();
}
if (!dataSnapshot.child("mobileNumber").equals(phone)) {
Toast.makeText(Registeration.this, "Entered phone is unique ", Toast.LENGTH_SHORT).show();
checkDup = false;
} else {
Toast.makeText(Registeration.this, "Entered phone is already registered", Toast.LENGTH_SHORT).show();
checkDup = true;
finish();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
private void CreateUserProfile() {
checkDuplicates();
if (checkDup == false) {
user.setName(Name.getText().toString());
user.setEmail(Email.getText().toString());
user.setUserName(UserName.getText().toString());
user.setMobileNumber(MobileNumber.getText().toString());
user.setPassword(Password.getText().toString());
user.setUser_Type(User_Type.getSelectedItem().toString());
ref.child(user.getUserName()).setValue(user).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Toast.makeText(Registeration.this,
"Registration Successful", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(Registeration.this,
"Registration Failed", Toast.LENGTH_LONG).show();
}
}
});
progressdialog.setMessage("Registering User...");
progressdialog.show();
startActivity(new Intent(Registeration.this, Staff_Home.class));
finish();
}
else {
finish();
}
}
these methods are what i used, i get the alerts that either one of the values is already registered but then it goes into inserting them and moves back to another activity. i want it to show the alerts and allow me to change them instead of doing it all over again.