gpt4 book ai didi

android - 如何使 FirebaseAuth.AuthStateListener 在 Kotlin 中工作?

转载 作者:行者123 更新时间:2023-12-01 19:04:31 27 4
gpt4 key购买 nike

class LoginActivity : AppCompatActivity() {

private val firebaseAuth = FirebaseAuth.getInstance()
private val firebaseAuthListener = FirebaseAuth.AuthStateListener {
val user = firebaseAuth.currentUser?.uid
user?.let {
startActivity(HomeActivity.newIntent(this))
finish()
}
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_login)
loginProgressLayout.setOnTouchListener { v, event -> true }
}

fun onLogin(v: View) {
var proceed = true
if (emailET.text.isNullOrEmpty()) {
emailTIL.error = "email is required"
emailTIL.isErrorEnabled = true
proceed = false
}
if(passwordET.text.isNullOrEmpty()) {
passwordTIL.error = "password is required"
passwordTIL.isErrorEnabled = true
proceed = false
}
if(proceed){
loginProgressLayout.visibility = View.VISIBLE
firebaseAuth.signInWithEmailAndPassword(emailET.text.toString(), passwordET.text.toString())
.addOnCompleteListener { task ->
if (!task.isSuccessful){
loginProgressLayout.visibility = View.GONE
Toast.makeText(this@LoginActivity, "LoginError", Toast.LENGTH_SHORT).show()
}
}
.addOnFailureListener { exception ->
exception.printStackTrace()
loginProgressLayout.visibility = View.GONE
}
}
} //onLogin end

我检查了我从 firebaseAuth.signInWithEmailAndPassword 代码行获得了一些身份验证号码。但我的问题是关于属性 FirebaseAuth.AuthStateListener,它不起作用。当我获得身份验证号码时,我希望 AuthStateListener 能够工作!

我阅读了 Firebase API,但它不起作用。如何使 FirebaseAuth.AuthStateListener 工作?

最佳答案

您需要使用监听器调用 addAuthStateListener 才能使其正常工作。

例如,在 Activity 的 onStart 中:

override fun onStart() {
super.onStart()
firebaseAuth!!.addAuthStateListener(this.firebaseAuthListener!!)
}

我建议研究这个答案(更多):Android Studio (Kotlin) - User has to log back into app every time app is closed

关于android - 如何使 FirebaseAuth.AuthStateListener 在 Kotlin 中工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59584456/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com