Firebase AuthUI not calling onActivityResult



我正在构建一个使用Firebase AuthUI来处理用户登录的Android应用程序。

implementation 'com.google.firebase:firebase-firestore:21.4.3'
implementation 'com.firebaseui:firebase-ui-auth:6.2.0'
implementation 'com.firebaseui:firebase-ui:6.2.0'

当我的活动启动并检查用户是否已登录时...如果没有,它调用startActivityForResult。请参阅下面的代码。

当AuthUI完成时,它永远不会调用ActivityResult。而是调用启动活动的 onRestart((。

关于如何调用活动结果的任何建议?

private fun startFirebaseAuthActivity() {
Timber.d("startFirebaseAuthActivity(): ")
EventBus.getDefault().post(MyEvents.Companion.StopListFragment())
// Choose authentication providers
val providers = arrayListOf(
AuthUI.IdpConfig.EmailBuilder().build(),
AuthUI.IdpConfig.GoogleBuilder().build()
)
// Create and launch sign-in intent
startActivityForResult(
AuthUI.getInstance()
.createSignInIntentBuilder()
.setIsSmartLockEnabled(false)
.setAvailableProviders(providers)
.build(),
RC_SIGN_IN
)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
Timber.d("onActivityResult(): ")
if (requestCode == RC_SIGN_IN) {
val response = IdpResponse.fromResultIntent(data)
if (resultCode == Activity.RESULT_OK && response != null) {
// Successfully signed in
auth = FirebaseAuth.getInstance()
newUser = User(auth!!.currentUser!!)
isNewUser = response.isNewUser
Timber.d("onActivityResult(): isNewUser=$isNewUser")
if (isNewUser) {
createNewUser()
} 
} else {
// Sign in failed.
// If response is null the user canceled the sign-in flow using the back button.
// Otherwise check response.getError().getErrorCode() and handle the error.
when {
response == null -> {
//If no response from the Server
Timber.d("onActivityResult(): sign_in_cancelled.")
showSnackbar(R.string.sign_in_cancelled)
}
response.error?.errorCode == ErrorCodes.NO_NETWORK -> {
//If there was a network problem the user's phone
Timber.d("onActivityResult(): no_internet_connection")
showSnackbar(R.string.no_internet_connection)
}
response.error?.errorCode == ErrorCodes.UNKNOWN_ERROR -> {
//If the error cause was unknown
Timber.e("onActivityResult(): unknown_error")
showSnackbar(R.string.unknown_error)
}
else -> {
Timber.e("onActivityResult(): unknown_sign_in_response")
showSnackbar(R.string.unknown_sign_in_response) //if the sign in response was unknown
}
}
startFirebaseAuthActivity()
}
}
}

我在类似于以下内容的代码中遇到了同样的问题

if (token?.isNotEmpty() == true) {
val activityIntent = Intent(this, MainActivity::class.java)
startActivity(activityIntent)
} else {
val loginIntent = AuthUI.getInstance()
.createSignInIntentBuilder()
.setAvailableProviders(providers)
.build()
startActivityForResult(loginIntent, RC_SIGN_IN)
}
finish()

问题是在 if else 块之外完成。 这是在从 AuthUI 活动返回结果之前完成我的活动。 将完成移动到令牌案例为我解决了问题

if (token?.isNotEmpty() == true) {
val activityIntent = Intent(this, MainActivity::class.java)
startActivity(activityIntent)
finish()
} else {
val loginIntent = AuthUI.getInstance()
.createSignInIntentBuilder()
.setAvailableProviders(providers)
.build()
startActivityForResult(loginIntent, RC_SIGN_IN)
}

我在您的代码中没有看到 finish((,但也许类似的东西正在返回结果之前完成活动。

相关内容

  • 没有找到相关文章

最新更新