如何解决如何在自定义Dialog类中设置Interface和listener,以将Facebook的登录回调传递给MainActivity中的firebase auth?
我在实现自定义对话框类时遇到了一些问题,该类从Facebook获得响应并将其传递回主要活动。
这是我的自定义课程:
class LoginDialog(var c: Activity) : Dialog(c),View.OnClickListener {
val mContext = c
private lateinit var realFbButton: LoginButton
private lateinit var listener: LoginListener
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
requestWindowFeature(Window.FEATURE_NO_TITLE)
getWindow()?.setBackgroundDrawableResource(android.R.color.transparent);
setContentView(R.layout.login_dialog)
// here image to cover the real facebook LoginButton
val loginFbButton: ImageView = findViewById(R.id.facebookButton)
loginFbButton.setOnClickListener(this)
// the real facebook login button I wanna click to start the facebook auth process
realFbButton = findViewById(R.id.facebooOfficalButton)
realFbButton.setOnClickListener(this)
}
override fun onClick(v: View) {
when (v.id) {
R.id.facebookButton -> {realFbButton.performClick()
// Initialize Facebook Login button
//TODO: (nothing necessary)
}
R.id.facebooOfficalButton -> {
realFbButton.setReadPermissions("email","public_profile")
// register callback after LoginButton has been pushed
realFbButton.registerCallback(callbackManager,object :
FacebookCallback<LoginResult>{
override fun onSuccess(result: LoginResult?) {
try{
Toast.makeText(context,"success",Toast.LENGTH_LONG).show()
// listener to pass result
listener.passLoginListener(result)
}
catch(e: Exception){
Toast.makeText(context,"exception" + e,Toast.LENGTH_LONG).show()
}
TODO("Not yet implemented")
}
override fun onCancel() {
TODO("Not yet implemented")
}
override fun onError(error: FacebookException?) {
TODO("Not yet implemented")
}
})
}
else -> {
Toast.makeText(mContext,"id not recognized: " + v,Toast.LENGTH_LONG).show()
}
}
dismiss()
}
fun setLoginListener(listener: LoginListener?) {
this.listener = listener!!
}
interface LoginListener{
public fun passLoginListener(callBack: LoginResult?)
}
}
在我的主要活动中:
// interface implementation
class CentralActivity : AppCompatActivity(),LoginDialog.LoginListener{/* my full class is here
...*/}
override fun passLoginListener(callBack: LoginResult?) {/*here call to handleFacebookAccessToken(token: AccessToken)*/}
// in onCreate overriden method:
val dialog = LoginDialog(this@CentralActivity)
dialog.setLoginListener(this@CentralActivity) // this activate the listener
但是它不起作用:即使使用调试器,我也可以绝对告诉任何东西都不能通过接口传递给活动,也不会触发“成功”响应(与其他响应一样)。
我该怎么做才能正确地做到这一点?
解决方法
我解决了这个问题,希望这种经历可以为其他人节省一些宝贵的时间:)
-
监听器需要正确初始化为自定义类,其中“ c”是从我的主要活动传递来的活动:
//in onCreate,inside the custom dialog class: listener = c as LoginDialog.LoginListener
-
由于回调需要一些时间才能返回,因此将响应传递给主要活动是没有用的:相反,最好将按钮传递给
override fun onClick(v: View) { when (v.id) { R.id.facebookButton -> {realFbButton.performClick() //TODO: nothing here } R.id.facebooOfficalButton -> { realFbButton.setReadPermissions("email","public_profile") this.listener.passLoginListener(realFbButton) // here we go passing the button to the interface and to the main activity } else -> { Toast.makeText(mContext,"id not recognized: " + v,Toast.LENGTH_LONG).show() } } dismiss()
在主要活动中, -
让我们将其设置为等待结果: 这是听众
override fun passLoginListener(fbb: LoginButton): LoginButton { // get the LoginButton and set up the receiver fbb.registerCallback(callbackManager,object : FacebookCallback<LoginResult> { override fun onSuccess(result: LoginResult?) { try{ Toast.makeText(applicationContext,"success",Toast.LENGTH_LONG).show() handleFacebookAccessToken(result!!.accessToken) } catch(e: Exception){ Toast.makeText(applicationContext,"exception" + e,Toast.LENGTH_LONG).show() } } override fun onCancel() { TODO("Not yet implemented") } override fun onError(error: FacebookException?) { TODO("Not yet implemented") } }) return fbb }
现在onActivityResult方法可以正常工作
override fun onActivityResult(requestCode: Int,resultCode: Int,data: Intent?) {
super.onActivityResult(requestCode,resultCode,data)
// Pass the activity result back to the Facebook SDK
callbackManager.onActivityResult(requestCode,data)}
现在,对于在onCreate中初始化的Firebase身份验证
//firebase auth in onCreate
auth = Firebase.auth
,并在此处进行了更新(有关此信息,请参见正式的Firebase文档)
private fun handleFacebookAccessToken(token: AccessToken) {
Log.d("Tag","handleFacebookAccessToken:$token")
Toast.makeText(applicationContext,"in handle fb login " + token,Toast.LENGTH_LONG).show()
val credential = FacebookAuthProvider.getCredential(token.token)
auth.signInWithCredential(credential)
.addOnCompleteListener(this) { task ->
if (task.isSuccessful) {
// Sign in success,update UI with the signed-in user's information
Log.d("Tag","signInWithCredential:success")
val user = auth.currentUser
updateUI(user)
} else {
// If sign in fails,display a message to the user.
Log.w("Tag","signInWithCredential:failure",task.exception)
Toast.makeText(baseContext,"Authentication failed.",Toast.LENGTH_SHORT).show()
updateUI(null)
}
// ...
}
}
希望有帮助!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。