Google新登录流程中onActivityResult()未触发问题求助
Hey there, let's walk through the common fixes for the Google Sign-In problems you're facing—specifically when switching accounts or using a device with no saved Google accounts, plus that pesky 12502 error.
1. Resolve the SIGN_IN_CURRENTLY_IN_PROGRESS (12502) Error
This error pops up when a sign-in flow is already running, and you accidentally trigger another one. Here's how to fix it:
- Add a boolean flag to track if a sign-in is in progress:
private var isSignInInProgress = false - Before launching the sign-in intent, check this flag:
if (!isSignInInProgress) { isSignInInProgress = true val signInIntent = googleSignInClient.signInIntent startActivityForResult(signInIntent, RC_SIGN_IN) } - Reset the flag once the sign-in flow completes (whether success or failure):
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { super.onActivityResult(requestCode, resultCode, data) isSignInInProgress = false // Handle sign-in result here }
If you're using the AndroidX Activity Result API (recommended for newer apps), reset the flag in the callback:
val signInLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> isSignInInProgress = false // Process result here }
2. Ensure Proper Handling of Account Switching
When users select "Use another account," your app might be caching the previous account. Force a fresh account picker with these steps:
- When the "Other Account" button is clicked, first sign out the current cached account:
googleSignInClient.signOut().addOnCompleteListener { task -> if (task.isSuccessful) { // Launch the sign-in intent again to show the account picker val signInIntent = googleSignInClient.signInIntent signInLauncher.launch(signInIntent) } } - Alternatively, configure your
GoogleSignInOptionsto not auto-select an account:val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) .requestIdToken(getString(R.string.default_web_client_id)) .requestEmail() .setAccountName(null) // Clear any cached account .build()
3. Verify Your Google Cloud Console Configuration
A lot of these issues boil down to misconfigured OAuth credentials:
- Double-check that you've added the correct SHA-1 fingerprint for both debug and release builds in the Google Cloud Console.
- Ensure your app's package name matches exactly what's listed in the OAuth 2.0 client ID configuration.
- Confirm that the
default_web_client_idin yourstrings.xmlmatches the client ID from the Cloud Console.
4. Migrate to the Activity Result API (If You Haven't Already)
The old onActivityResult() method can be unreliable in some cases, especially with fragments. Switching to the modern API ensures callbacks are triggered consistently:
- Register the launcher in your
onCreate()method:private lateinit var signInLauncher: ActivityResultLauncher<Intent> override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // ... other setup signInLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> if (result.resultCode == Activity.RESULT_OK) { val task = GoogleSignIn.getSignedInAccountFromIntent(result.data) handleSignInResult(task) } } } - Launch the sign-in intent using the launcher instead of
startActivityForResult():signInLauncher.launch(googleSignInClient.signInIntent)
5. Add Debug Logs to Pinpoint Exact Issues
Logs will help you see what's going on behind the scenes. Add logging in key places:
- When launching the sign-in intent.
- Inside the sign-in result callback:
private fun handleSignInResult(completedTask: Task<GoogleSignInAccount>) { try { val account = completedTask.getResult(ApiException::class.java) // Success, proceed with authentication } catch (e: ApiException) { Log.e(TAG, "Sign-in failed: ${e.statusCode} - ${e.message}") // This will show you if you're hitting 12502 or other status codes } }
内容的提问来源于stack exchange,提问作者torchhound

