others-how to solve onActivityResult not called problem in Google Sign-In API?

1. Purpose

If you’re facing an issue where the onActivityResult method is not being called after using the Google Sign-In API, this post will help you solve it.



2. Solution

2.1 Probem details

I have written a utility class named GoogleSignInUtils.java that encapsulates a signIn method required for Google SignIn process. However, after the sign-in process is completed successfully, the onActivityResult method does not get called as expected.

In my GoogleSignInUtils.java, there is a signIn method ,which is called when user click the Sign in google button.

    public static void signIn(Activity activity) {
        gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestServerAuthCode(CLIENT_ID)
                .requestEmail()
                .requestId()
                .requestScopes(new Scope(GmailScopes.GMAIL_SEND),
                        new Scope(Scopes.EMAIL))
                .build();
        mGoogleSignInClient = GoogleSignIn.getClient(activity, gso);
        oneTapClient = Identity.getSignInClient(activity);

        activity.startActivityForResult(getGoogleIntent(), RC_SIGN_IN);
    }

When the signin process succeeds, the following onActivityResult should be called:

    public static void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
        if (requestCode == RC_SIGN_IN) {
            Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
            try {
                GoogleSignInAccount account = task.getResult(ApiException.class);
                //...

            } catch (ApiException e) {
                Log.w(TAG, "Sign-in failed", e);
            } catch (Exception e) {
                LogUtils.error("",e);
            }
        }
    }

But when user click Google Sign-in button, the signIn method is called ,but after login success, the onActivityResult is not called. why?

2.2 The reason

For my situation, the onActivityResult is not called because it exist in my GoogleSignInUtils.java, not in my MyActivity.java,

We should implement the onActivityResult in MyActivity.java , which should call GoogleSignInUtils.java as follows:

To fix this issue, you need to implement the onActivityResult method in your activity class that started the sign-in process. Then, you can call the onActivityResult method in your GoogleSignInUtils.java class from your activity as shown below:

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

        GoogleSignInUtils.onActivityResult(this,requestCode,resultCode,data);

    }

This will ensure that the onActivityResult method gets called and your app can handle the sign-in result as expected.

2.3 Other possible reasons

torchhound on stackoverflow said that:

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.

Zahur Sh said:

I had android:noHistory=”true” attribute in my LoginActivity which caused this issue in lollipop, but worked fine in android 8

And Robert said:

Managed to figure out what the problem was… In my manifest I have declared my Activity as android:noHistory=”false” and this was the problem. After setting it to android:noHistory=”true” for my activity and firing my Intentthe following way it worked like a charm.

To conclude, the following reasons might cause the problem:

  • Missing Intent flags such as Intent.FLAG_ACTIVITY_NO_HISTORY.
  • The android:noHistory=”true” attribute in the activity declaration in the Android manifest file.
  • Improper implementation of the sign-in flow.



3. Summary

In this post, we discussed how to fix the issue where the onActivityResult method is not being called in the Google Sign-In API. By implementing the onActivityResult method in your activity class and calling it from your utility class, you can handle the sign-in result as expected.