M
M
Mark_Brown1162018-08-23 15:57:19
Android
Mark_Brown116, 2018-08-23 15:57:19

Why does it give an error? If you click on "ok" then the second activity opens, how to fix it?

I have authentication. If the SMS code is successful, the message "login successful" appears, which is certainly good, only the second activity is missing in the application, I added it using this method (Intent intent = new Intent(MainActivity.this, Main2Activity.class);
startActivity(intent ); from that moment it all started. Even with unsuccessful authentication, there will still be a transition. How to fix it? What conditions to set?

editTextCode = findViewById(R.id.editTextCode);
        editTextPhone = findViewById(R.id.editTextPhone);

        findViewById(R.id.buttonGetVerificationCode).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendVerificationCode();
            }
        });


        findViewById(R.id.buttonSignIn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, Main2Activity.class);
                startActivity(intent);
                verifySignInCode();
            }
        });
    }

    private void verifySignInCode(){
        String code = editTextCode.getText().toString();
        PhoneAuthCredential credential = PhoneAuthProvider.getCredential(codeSent, code);
        signInWithPhoneAuthCredential(credential);
    }

    private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
        mAuth.signInWithCredential(credential)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {
                            //here you can open new activity
                            Toast.makeText(getApplicationContext(),
                                    "Вход Успешен", Toast.LENGTH_LONG).show();
                        } else {
                            if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
                                Toast.makeText(getApplicationContext(),
                                        "Неверный Проверочный Код ", Toast.LENGTH_LONG).show();
                            }
                        }
                    }
                });
    }

    private void sendVerificationCode(){

        String phone = editTextPhone.getText().toString();

        if(phone.isEmpty()){
            editTextPhone.setError("Phone number is required");
            editTextPhone.requestFocus();
            return;
        }

        if(phone.length() < 10 ){
            editTextPhone.setError("Please enter a valid phone");
            editTextPhone.requestFocus();
            return;
        }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mark_Brown116, 2018-08-23
@Mark_Brown116

xQfx-8uxh5g.jpgFdn026wAEQs.jpg

M
Mikhail Chvarkov, 2018-08-23
@KuSu

instead of //here you can open new activity
do

Intent intent = new Intent(MainActivity.this, Main2Activity.class);
 startActivity(intent);
 verifySignInCode();

and on the button, call only verifySignInCode()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question