【问题标题】:Firebase AuthUI not calling onActivityResultFirebase AuthUI 未调用 onActivityResult
【发布时间】:2020-10-02 22:08:24
【问题描述】:

我正在构建一个使用 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 完成时,它永远不会调用 onActivityResult。而是调用启动活动的 onRestart()。

关于如何调用 onActivityResult 有什么建议吗?

   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()
            }
        }
    }

【问题讨论】:

  • 我没听明白你能告诉我你为什么从onActivityResult()打电话给startFirebaseAuthActivity()
  • 我没有从 onActivityResult() 调用 startFirebaseAuthActivity()。在我的启动 Activity 的 onStart() 方法(未显示)中,我检查用户是否登录到 Firebase。如果没有,则调用 startFirebaseAuthActivity(),然后通过 startActivityForResult 调用 AuthUI。我的期望是,当 AuthUI 完成时,我的启动 Activity 通过 onActivityResult 恢复......但它没有......它通过 onRestart() 恢复。
  • 您找到解决方案了吗?

标签: android firebase firebase-authentication firebaseui


【解决方案1】:

我遇到了同样的问题,代码类似于以下

    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(),但可能类似的东西正在完成活动,然后才能返回结果。

【讨论】:

    猜你喜欢
    • 2018-08-06
    • 2015-01-15
    • 2019-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-10
    • 1970-01-01
    相关资源
    最近更新 更多