Fail call when trying to recover auth with backgrounded app (#960) We noticed some Android (native) crashes in our application when we're trying to call the get token method when our app is in the background. This is easy to prevent by returning a failed result.
diff --git a/packages/google_sign_in/android/src/main/java/io/flutter/plugins/googlesignin/GoogleSignInPlugin.java b/packages/google_sign_in/android/src/main/java/io/flutter/plugins/googlesignin/GoogleSignInPlugin.java index ef6d4c0..d2fc260 100755 --- a/packages/google_sign_in/android/src/main/java/io/flutter/plugins/googlesignin/GoogleSignInPlugin.java +++ b/packages/google_sign_in/android/src/main/java/io/flutter/plugins/googlesignin/GoogleSignInPlugin.java
@@ -469,12 +469,19 @@ } catch (ExecutionException e) { if (e.getCause() instanceof UserRecoverableAuthException) { if (shouldRecoverAuth && pendingOperation == null) { - checkAndSetPendingOperation(METHOD_GET_TOKENS, result, email); - Intent recoveryIntent = - ((UserRecoverableAuthException) e.getCause()).getIntent(); - registrar - .activity() - .startActivityForResult(recoveryIntent, REQUEST_CODE_RECOVER_AUTH); + Activity activity = registrar.activity(); + if (activity == null) { + result.error( + ERROR_USER_RECOVERABLE_AUTH, + "Cannot recover auth because app is not in foreground. " + + e.getLocalizedMessage(), + null); + } else { + checkAndSetPendingOperation(METHOD_GET_TOKENS, result, email); + Intent recoveryIntent = + ((UserRecoverableAuthException) e.getCause()).getIntent(); + activity.startActivityForResult(recoveryIntent, REQUEST_CODE_RECOVER_AUTH); + } } else { result.error(ERROR_USER_RECOVERABLE_AUTH, e.getLocalizedMessage(), null); }