Android Kotlin中AWS Amplify登录onResult回调未触发问题排查
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.gradlefor Amplify dependencies—try rolling back to the version that was working for you. For example, if you upgraded to a neweraws-android-sdk-mobileclient, pin it to a stable older version like2.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
signInonly after theinitializecallback 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
runOnUiThreadcan 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_AUTHwas 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
signInmethod 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.
5. Lifecycle-Related Callback Loss
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

