Kalybor
Kalybor

Reputation: 51

Toast and new intent call not working inside firebase onComplete

When i call new intent in my LoginFragment it works.

        binding.textView2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //TODO: ce klices iz fragmenta mores tako definirati intent!
            Intent intent = new Intent(getActivity(), RegisterActivity.class);
            startActivity(intent);

        }
    });

The same works for toasting. You just have to write getActivity() instead of RandomActivity.class in the Intent/Toast definition.

But when i try to do the same thing inside of firebase login result it doesnt work.

    binding.buttonLogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Editable emailE = binding.editTextTextEmailAddressLogin.getText();
            String email = emailE.toString();

            Editable pwE = binding.editTextTextPasswordLogin.getText();
            String password = pwE.toString();

            mAuth.signInWithEmailAndPassword(email, password)
                    .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                        @Override
                        public void onComplete(@NonNull Task<AuthResult> task) {
                            if (task.isSuccessful()) {
                                // Sign in success, update UI with the signed-in user's information
                                Log.d(TAG, "signInWithEmail:success");

                                //TODO: update user interface!!
                                FirebaseUser user = mAuth.getCurrentUser();
                                //updateUI(user);
                            } else {
                                // If sign in fails, display a message to the user.
                                Log.w(TAG, "signInWithEmail:failure", task.getException());

                              Toast.makeText(getActivity(), "Authentication failed.",
                                        Toast.LENGTH_SHORT).show();
                                //updateUI(null);
                            }

                            // ...
                        }
                    });

        }
    });

Error is:

Cannot resolve method 'addOnCompleteListener(anonymous android.view.View.OnClickListener, anonymous com.google.android.gms.tasks.OnCompleteListener<com.google.firebase.auth.AuthResult>)'

Why cant i call the toast inside of the firebases onComplete function?

And for some reason I cant even remove the toast, it appears it has to be defined in onComplete?

Upvotes: 1

Views: 143

Answers (2)

Frank van Puffelen
Frank van Puffelen

Reputation: 599946

As the error message says, the this in your call is a OnClickListener and not a Context.

mAuth.signInWithEmailAndPassword(email, password)
     .addOnCompleteListener(this, new OnCompleteListener...

You can either capture the context in a variable (as shown in Correct context to use within callbacks) or use something like MainActivity.this or getActivity().

Upvotes: 1

Kalybor
Kalybor

Reputation: 51

 mAuth.signInWithEmailAndPassword(email, password)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {

I had to change this to getActivity()

 mAuth.signInWithEmailAndPassword(email, password)
                .addOnCompleteListener(getActivity(), new OnCompleteListener<AuthResult>() {

Upvotes: 1

Related Questions