In my app, First, I am verifying phone numbers by using phone authentication of firebase
.
I am doing this process in the OTPVerificationActivity
class. Every thing is working fine in this class, I am getting the OTP
code also and the user gets signed in.
If the phone number is verified, then I am verifying the email by sending the verification email. I am doing this process in the EmailVerificationActivity
class. In this class first, i am checking is the user exists or not. if the user is there, then I am logging out the user.
Then i create the account using createUserWithEmailAndPassword
function. After this, i want to send a verification email to the user.
PROBLEM:- Now the problem is, after creating the account,onCreate
method gets called again. Due to this, I am unable to send the verification email and the further task. So how to stop onCreate
method from getting called again.
NOTE :- I tried to run only
EmailVerificationActivity
notOTPVerificationActivity
. In that case every thing was working properly. I was able to send verification email and also able to verify the user. But when i am using theOTPVerificationActivity
andEmailVerificationActivity
together i am getting this problem. So please help me to solve this problem.
public class OTPVerificationActivity extends AppCompatActivity {
String verificationID;
TextView messageTV;
EditText OTPET;
FirebaseAuth mAuth;
FirebaseAuth.AuthStateListener mAuthStateListener;
PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallBack = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
@Override
public void onVerificationCompleted(@NonNull PhoneAuthCredential phoneAuthCredential) {
signInWithCredential(phoneAuthCredential);
}
@Override
public void onVerificationFailed(@NonNull FirebaseException e) {
}
@Override
public void onCodeSent(@NonNull String s, @NonNull PhoneAuthProvider.ForceResendingToken forceResendingToken) {
super.onCodeSent(s, forceResendingToken);
verificationID = s;
Log.i("onSend",s);
}
};
String phoneNumber;
public void sendVerificationCode(View view)
{
String code = OTPET.getText().toString();
PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationID,code);
signInWithCredential(credential);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_otpverification);
messageTV = (TextView) findViewById(R.id.messageTextView);
OTPET = (EditText) findViewById(R.id.OTPEditText);
mAuth = FirebaseAuth.getInstance();
mAuthStateListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
if(firebaseAuth.getCurrentUser() != null)
{
firebaseAuth.signOut();
if(firebaseAuth.getCurrentUser() == null)
{
Intent intent = new Intent(OTPVerificationActivity.this,EmailVerification.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
}
else
{
Log.i("firebaseAuth","user is not null");
}
}
}
};
String message = "Please type the verification code sent to ";
messageTV.setText(message+" " + "+91 " + getIntent().getStringExtra("phoneNumber"));
phoneNumber = "+91" + getIntent().getStringExtra("phoneNumber");
sendPhoneNumber(phoneNumber);
}
@Override
protected void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthStateListener);
}
@Override
protected void onDestroy() {
super.onDestroy();
if(mAuth.getCurrentUser() != null)
{
mAuth.signOut();
}
}
public void sendPhoneNumber(String number)
{
Log.i("sendPhoneNumber","inside it");
PhoneAuthProvider.getInstance().verifyPhoneNumber(
number,
60,
TimeUnit.SECONDS,
TaskExecutors.MAIN_THREAD,
mCallBack
);
Log.i("sendPhoneNumber","outside it");
}
private void signInWithCredential(final PhoneAuthCredential credential)
{
mAuth.signInWithCredential(credential).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(!task.isSuccessful())
{
Toast.makeText(OTPVerificationActivity.this, "Sign in Failed.", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(OTPVerificationActivity.this, "Phone number verified.", Toast.LENGTH_SHORT).show();
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.i("FailedListener",e.getMessage());
}
});
}
}
Below is the code of
EmailVerificationActivity
class.
public class EmailVerification extends AppCompatActivity {
EditText emailET, passwordET, confirmPasswordET;
AlertDialog.Builder builder;
AlertDialog alertDialog;
ProgressDialog progressDialog;
FirebaseAuth mAuth;
FirebaseAuth.AuthStateListener mAuthStateListener;
public void login(View view)
{
startActivity(new Intent(EmailVerification.this,MainActivity.class));
}
public void submit(View view)
{
String email = emailET.getText().toString();
String password = passwordET.getText().toString();
String confirmPassword = confirmPasswordET.getText().toString();
if(!(TextUtils.isEmpty(email) && TextUtils.isEmpty(password)))
{
if(password.equals(confirmPassword))
{
progressDialog.show();
mAuth.createUserWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful())
{
//acount created...
mAuth.getCurrentUser().sendEmailVerification().addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
Log.i("verificationEmail","send");
progressDialog.dismiss();
if(!EmailVerification.this.isFinishing())
{
alertDialog.show();
}
Toast.makeText(EmailVerification.this, "Email send successfully.", Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.i("verificationEmail","Not send");
//failed to create account...
progressDialog.dismiss();
Toast.makeText(EmailVerification.this, "Failed to create account", Toast.LENGTH_SHORT).show();
}
});
}
else
{
//account not created...
progressDialog.dismiss();
Log.i("taskNotSuccessful",task.getException().getMessage());
Toast.makeText(EmailVerification.this, "Problem in creating account", Toast.LENGTH_SHORT).show();
}
}
});
}
else
{
Toast.makeText(this, "Confirm Password is not same as Password.", Toast.LENGTH_SHORT).show();
}
}
else
{
Toast.makeText(this, "Fields are empty.", Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_email_verification);
Log.i("onCreate","inside it");
initializeWidgets();
progressDialog = new ProgressDialog(EmailVerification.this);
progressDialog.setMessage("Sending verification email.");
builder= new AlertDialog.Builder(EmailVerification.this);
builder.setTitle("Email Verification")
.setMessage("We sent you a verification email, Please verify email before moving further.")
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(EmailVerification.this,SignInActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
}
});
alertDialog = builder.create();
alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
alertDialog.getButton(DialogInterface.BUTTON_POSITIVE).setTextColor(R.style.MyProgressDialogStyle);
}
});
mAuth = FirebaseAuth.getInstance();
mAuthStateListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
if(firebaseAuth.getCurrentUser() != null)
{
// user is signed up...
Log.i("mAuthStateChanged","user is not null.");
}
else
{
Log.i("mAuthStateChanged","user is null");
}
}
};
}
@Override
protected void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthStateListener);
}
private void initializeWidgets()
{
emailET = (EditText) findViewById(R.id.EmailEditText);
passwordET = (EditText) findViewById(R.id.PasswordEditText);
confirmPasswordET = (EditText) findViewById(R.id.ConfirmPasswordEditText);
}
}