You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Kotlin中AWS Amplify登录onResult回调未触发问题排查

AWS Amplify SignIn Callback onResult Not Triggering in Kotlin Android App

Hey, I've run into a similar issue with AWS Amplify's login callback getting stuck before, so let's walk through some possible causes and fixes based on your scenario:

1. SDK Version Compatibility Issues

Since you mentioned the code worked before, a recent SDK update might have introduced compatibility bugs:

  • Check your app-level build.gradle for Amplify dependencies—try rolling back to the version that was working for you. For example, if you upgraded to a newer aws-android-sdk-mobileclient, pin it to a stable older version like 2.33.0 (adjust based on your previous working version).
  • Make sure all Amplify-related dependencies use the same version to avoid version conflicts that can break callback logic.

2. Initialization & Threading Problems

  • Ensure AWSMobileClient is fully initialized: Sometimes login calls are made before the client finishes setting up. Double-check that you're calling signIn only after the initialize callback completes. Here's a quick example of proper initialization:
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_login)
    
        AWSMobileClient.getInstance().initialize(applicationContext, object : Callback<UserStateDetails> {
            override fun onResult(result: UserStateDetails) {
                Log.d("DEBUG", "Client initialized: ${result.userState}")
                // Now safe to handle login actions
            }
    
            override fun onError(e: Exception) {
                Log.e("DEBUG", "Initialization failed", e)
            }
        })
    }
    
  • Test without runOnUiThread first: While updating UI requires the main thread, sometimes wrapping the entire callback logic in runOnUiThread can mask whether the callback is actually firing. Try logging the sign-in state first in the background thread, then switch to UI only for UI updates.

3. AWS Cognito User Pool Configuration Changes

It's possible your Cognito User Pool settings were modified recently:

  • Log into the AWS Console and check your user pool's App Client Settings and Authentication Flow Settings. For example, if ALLOW_ADMIN_USER_PASSWORD_AUTH was disabled or a new MFA requirement was added, the login flow might behave differently than your code expects.
  • Verify that your app client's authentication flows match what your code is using (the signIn method you're calling uses the standard user password auth flow by default).

4. Enhanced Logging for Debugging

Turn on verbose logging to see more details about what's happening under the hood:

// Add this before initializing AWSMobileClient
AWSLogger.logger = AWSLogLevel.VERBOSE

This will show you full request/response details, including if the network request is failing silently, or if an exception is being caught internally by the SDK instead of triggering onError.

Also, double-check your device's network—make sure there's no proxy, VPN, or firewall blocking traffic to AWS endpoints.

If your Activity/Fragment gets destroyed (e.g., screen rotation, background app kill) while the login request is in flight, the callback context might become invalid. Try:

  • Using a ViewModel to host your login logic, so the callback isn't tied to the Activity's lifecycle.
  • Ensuring the login screen is kept alive (e.g., handle configuration changes manually) while the request is processing.

Here's a slightly optimized version of your login code to test with:

loginBtn.setOnClickListener {
    val username = usernameInput.text.toString().trim()
    val password = passwordInput.text.toString().trim()

    AWSMobileClient.getInstance().signIn(username, password, null, object : Callback<SignInResult> {
        override fun onResult(signInResult: SignInResult) {
            // First confirm the callback fires by logging here
            Log.d("DEBUG", "Sign-in state received: ${signInResult.signInState}")
            
            runOnUiThread {
                when (signInResult.signInState) {
                    SignInState.DONE -> {
                        Log.d("DEBUG", "Login successful!")
                        // Navigate to home screen
                    }
                    SignInState.SMS_MFA -> showSmsMfaPrompt()
                    SignInState.NEW_PASSWORD_REQUIRED -> showNewPasswordScreen()
                    else -> Log.w("DEBUG", "Unexpected state: ${signInResult.signInState}")
                }
            }
        }

        override fun onError(e: Exception) {
            Log.e("DEBUG", "Login failed", e)
            runOnUiThread {
                // Show error message to user
            }
        }
    })
}

内容的提问来源于stack exchange,提问作者YoanGJ

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 09:05:03