Issue with OnCompleteListener<AuthResult> in Kotlin

Viewed 51

I have this WORKING code in Kotlin

val listener = View.OnClickListener {
            progressDialog.show()
            auth.signInWithEmailAndPassword(etEmail.text.toString(),etPassword.text.toString())
                .addOnCompleteListener{
                        progressDialog.dismiss()
                        if (it.isSuccessful){
                            Toast.makeText(this@SignInActivity,"Logged In",Toast.LENGTH_LONG).show()
                            val intent = Intent(this@SignInActivity,MainActivity::class.java)
                            startActivity(intent)
                        } else{
                            Toast.makeText(this@SignInActivity, it.exception?.message,Toast.LENGTH_LONG).show()
                        }

                }
        }
        btnSigIn.setOnClickListener(listener)

If I add OnCompleteListener<AuthResult> like below

val listener = View.OnClickListener {
            progressDialog.show()
            auth.signInWithEmailAndPassword(etEmail.text.toString(),etPassword.text.toString())
                .addOnCompleteListener{
                    OnCompleteListener<AuthResult> { task ->
                        progressDialog.dismiss()
                        if (task.isSuccessful){
                            Toast.makeText(this@SignInActivity,"Logged In",Toast.LENGTH_LONG).show()
                            val intent = Intent(this@SignInActivity,MainActivity::class.java)
                            startActivity(intent)
                        } else{
                            Toast.makeText(this@SignInActivity, task.exception?.message,Toast.LENGTH_LONG).show()
                        }
                    }
                }
        }
        btnSigIn.setOnClickListener(listener)

it doesn't show any error but DOESN'T work (doesn't log in.) My question is what could be the reason? Thanks.

1 Answers

You have brackets {} around your listener, so you've created a lambda that returns a listener, and that's what you're passing to addOnCompleteListener. When the lambda is called on completion, it will create your inner listener and do nothing with it, then return.

Related