I am trying to verify a phone number using firebase phone number authentication. I have 3 activities. First one is "EnterMobile", second one is "OtpVerification" and last one is "dashboard". I am able to send OTP but when I enter OTP and click on verify button, it goes back to first activity instead of dashboard activity.
public class OtpVerification extends AppCompatActivity {
private EditText inputotp1, inputotp2, inputotp3, inputotp4, inputotp5, inputotp6;
private String getotpbackend;
public OtpVerification() {
super();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_otp_verification);
inputotp1 = findViewById(R.id.inputotp1);
inputotp2 = findViewById(R.id.inputotp2);
inputotp3 = findViewById(R.id.inputotp3);
inputotp4 = findViewById(R.id.inputotp4);
inputotp5 = findViewById(R.id.inputotp5);
inputotp6 = findViewById(R.id.inputotp6);
getotpbackend = getIntent().getStringExtra("backendOtp");
final ProgressBar progressBarSendOtp = findViewById(R.id.sending_otp_progress_bar);
final Button submitOtp = findViewById(R.id.submitOtp);
TextView ShowMobile = findViewById(R.id.ShowMobile);
Intent intent = getIntent();
ShowMobile.setText(String.format(
"+971-%s", intent.getStringExtra("mobile")
));
submitOtp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!inputotp1.getText().toString().trim().isEmpty() && !inputotp2.getText().toString().trim().isEmpty() && !inputotp3.getText().toString().trim().isEmpty() && !inputotp4.getText().toString().trim().isEmpty() && !inputotp5.getText().toString().trim().isEmpty() && !inputotp6.getText().toString().trim().isEmpty()) {
String entercodeotp = inputotp1.getText().toString() +
inputotp2.getText().toString() +
inputotp3.getText().toString() +
inputotp4.getText().toString() +
inputotp5.getText().toString() +
inputotp6.getText().toString();
if (getotpbackend != null) {
progressBarSendOtp.setVisibility(View.VISIBLE);
submitOtp.setVisibility(View.INVISIBLE);
PhoneAuthCredential phoneAuthCredential = PhoneAuthProvider.getCredential(
getotpbackend, entercodeotp
);
FirebaseAuth.getInstance().signInWithCredential(phoneAuthCredential)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
progressBarSendOtp.setVisibility(View.GONE);
submitOtp.setVisibility(View.VISIBLE);
if (task.isSuccessful()) {
Intent intent = new Intent(getApplicationContext(), dashboard.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
} else {
Toast.makeText(OtpVerification.this, "Enter Correct OTP", Toast.LENGTH_SHORT).show();
}
}
});
} else {
Toast.makeText(OtpVerification.this, "Please Check Internet Connection", Toast.LENGTH_SHORT).show();
}
// Toast.makeText(OtpVerification.this, "otp verify", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(OtpVerification.this, "Please enter all OTP number", Toast.LENGTH_SHORT).show();
}
}
});
numberOtpMove();
TextView ResendLabel = findViewById(R.id.SendAgain);
ResendLabel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PhoneAuthProvider.getInstance().verifyPhoneNumber(
"+971" + getIntent().getStringExtra("mobile"),
60,
TimeUnit.SECONDS,
OtpVerification.this,
new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
@Override
public void onVerificationCompleted(@NonNull PhoneAuthCredential phoneAuthCredential) {
Intent intent = new Intent(getApplicationContext(), dashboard.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
}
@Override
public void onVerificationFailed(@NonNull @NotNull FirebaseException e) {
Toast.makeText(OtpVerification.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
@Override
public void onCodeSent(@NonNull @NotNull String newbackendotp, @NonNull PhoneAuthProvider.ForceResendingToken forceResendingToken) {
super.onCodeSent(newbackendotp, forceResendingToken);
getotpbackend = newbackendotp;
Toast.makeText(OtpVerification.this, "OTP Sent Again", Toast.LENGTH_SHORT).show();
}
}
);
}
});
}
private void numberOtpMove() {
inputotp1.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (!s.toString().trim().isEmpty()) {
inputotp2.requestFocus();
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
inputotp2.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (!s.toString().trim().isEmpty()) {
inputotp3.requestFocus();
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
inputotp3.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (!s.toString().trim().isEmpty()) {
inputotp4.requestFocus();
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
inputotp4.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (!s.toString().trim().isEmpty()) {
inputotp5.requestFocus();
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
inputotp5.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (!s.toString().trim().isEmpty()) {
inputotp6.requestFocus();
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
}