4

Explanation:

I've implemented the Google Sign In flow according to the official tutorial and only accounts that are already signed into the device are authenticated properly. When a user chooses "Use Another Account" or there are no Google accounts stored on the device once the user's credentials have been entered they are shown the login screen again but onActivityResult() is not triggered. Once I did get a GoogleSignInStatusCode 12502 which is SIGN_IN_CURRENTLY_IN_PROGRESS when I was returned to the login screen and tried to tap the Google sign in button again but most of the time it just authenticates normally at that point or doesn't do anything onClick.

Abbreviated Code:

case R.id.googleLoginButton:
            googleSignIn();
            break;

public void googleSignIn() {
    mIsResolving = true;
    Intent signInIntent = mGoogleSignInClient.getSignInIntent();
    this.startActivityForResult(signInIntent, RC_GOOGLE);
}

 @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    callbackManager.onActivityResult(requestCode, resultCode, data);
    super.onActivityResult(requestCode, resultCode, data);

    switch (requestCode) {
        case RC_HINT:
            // Drop into handling for RC_READ
        case RC_READ:
            if (resultCode == RESULT_OK) {
                boolean isHint = (requestCode == RC_HINT);
                Credential credential = data.getParcelableExtra(Credential.EXTRA_KEY);
                processRetrievedCredential(credential, isHint);
            } else if (resultCode == 1001){
                //user chose a different account
            } else {
                Log.e(TAG, "Credential Read: " + resultCode);
                Toast.makeText(this, "Credential Read Failed", Toast.LENGTH_LONG).show();
                Crashlytics.log("Credential Read Failed");
            }

            mIsResolving = false;
            break;
        case RC_SAVE_PASSWORD_AUTH:
            if (resultCode == RESULT_OK) {
                return;
            } else {
                Log.e(TAG, "Credential Save: NOT OK");
                Toast.makeText(this, "Credential Save Failed", Toast.LENGTH_LONG).show();
                Crashlytics.log("Credential Save Failed");
            }

            mIsResolving = false;
            break;
        case RC_GOOGLE:
            Log.d(TAG, "RC_GOOGLE");
            Log.d(TAG, resultCode + "");
            Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
            handleSignInResult(task);
            break;
        default:
            Log.d(TAG, "default case onActivityResult, requestCode " + requestCode + ", resultCode " + resultCode );
            Crashlytics.log("default case onActivityResult, requestCode " + requestCode + ", resultCode " + resultCode );
            break;
    }
}

Troubleshooting I've Tried:

I had to change android:noHistory to false in my AndroidManifest.xml to allow the app to return to the login screen. Also tried startActivityForResult() with and without this. I've tried variations with setResult() instead of startActivityForResult, tried several Intent flags, checked my manifest for xml tags that might be obstructing onActivityResult(), checked SplashScreen, the only Activity in front of LoginSignupActivity, for code that might block onActivityResult(), added Log.ds which lead to the realization that onActivityResult was not being called in this case but worked in all other situations, tried with and without callbackManager, for Facebook auth, in onActivityResult(), and tried removing Smart Lock and various other pieces of code which had no effect.

torchhound
  • 510
  • 6
  • 15
  • It's very hard to tell from your code why this isn't working - as soon as you use `startActivityForResult` you are passing the responsibility for the `setResult` to the intent recipient. Are you calling `startActivityForResult` from an `Activity` or `Fragment` and do you have the correct deps in your gradle file? – Mark Feb 10 '18 at 22:36
  • I am calling `startActivityForResult` from an `AppCompatActivity`. I should have all the right dependencies since Google auth works in the case of a user already having an account on their device. I am applying `com.google.gms.google-services` and have `implementation 'com.google.android.gms:play-services-auth:11.8.0'` in my module Gradle. I also have an up-to-date `google-services.json` and my SHA1 debug key is active with Google and Firebase. – torchhound Feb 10 '18 at 22:57
  • for some reason I would think that `android:noHistory=false` is not right. https://stackoverflow.com/a/28246350/4252352 – Mark Feb 10 '18 at 23:07
  • I tried every combination of `android:noHistory` for `SplashScreen` and `LoginSignupActivity`. The only change was that when `SplashScreen` `android:noHistory` is set to true the sign in flow dumps you at the app drawer or home screen instead of taking you back to the app. – torchhound Feb 10 '18 at 23:17
  • Hi, anyone has better answer ? I don't found any `onHistory` or `FLAG_ACTIVITY_NO_HISTORY` in my code. – 林果皞 Apr 09 '18 at 11:18
  • Below is the response that I have received on the open/known google sign in issue. I'm afraid there is nothing much that we can do here other than wait and watch.. https://github.com/firebase/FirebaseUI-Android/issues/1297 – Rohan Patel May 23 '18 at 06:08

2 Answers2

1

I missed an Intent flag of Intent.FLAG_ACTIVITY_NO_HISTORY that was added in a utility function that was called from my splash screen.

torchhound
  • 510
  • 6
  • 15
  • Can you please explain this. I am facing the same issue – Jithin Sunny Mar 20 '18 at 09:29
  • Please be more specific? I can't find any places that have the line **intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);** (my project and GMS libraries) – Phong Nguyen May 01 '18 at 10:29
  • I facing the same issue, please help: https://stackoverflow.com/questions/50114176/google-sign-in-doesnt-return-any-result-when-use-another-account-to-sign-in – Phong Nguyen May 01 '18 at 10:32
  • 3
    So you mark your own answer with just a link down to your blog?? Even the dramatic lines does not explains the solution. – Koushik Shom Choudhury May 15 '18 at 08:28
  • This is a bug on Android. https://github.com/googlesamples/google-services/issues/358 – Haris May 24 '18 at 18:12
  • 3
    Thank you very much. I had `android:noHistory="true"` attribute in my LoginActivity which caused this issue in lollipop, but worked fine in android 8 – Zahur Sh Dec 19 '18 at 06:31
0

Remove android:noHistory="true" from your gmail sign in activities manifest file.

This works for me.

Ahamadullah Saikat
  • 4,437
  • 42
  • 39