1

I want to manually handle the connect() and disconnect() operation on the GoogleApiClient. I'm trying to:

  • build a new GoogleApiClient (without enableAutoManage)
  • call connect()
  • when onConnected() is called perform signOut
  • call disconnect() after signOut is over

Here's an example:

fun signOut(googleApiClient: GoogleApiClient, resultCallback: (Status) -> Unit) {
    Auth.GoogleSignInApi.signOut(googleApiClient).setResultCallback(resultCallback)
}

fun test() {
    val googleApiClient = GoogleApiClient.Builder(activity)
        .addApi(Auth.GOOGLE_SIGN_IN_API, buildGoogleSignInOptions(googleAuthId))
        .build()
    googleApiClient.registerConnectionCallbacks(object : ConnectionCallbacks {
        override fun onConnected(connectionHint: Bundle?) {
            signOut { status ->
                //TODO something with status
                googleApiClient.disconnect()
            }
        }

        override fun onConnectionSuspended(cause: Int) {
            //nop
        }
    })
    googleApiClient.registerConnectionFailedListener {
        //TODO handle failure
    }
    googleApiClient.connect()
}

However when onConnected() is called the signOut call fails with

IllegalStateException: GoogleApiClient is not connected yet 

Am I doing something wrong or is it a bug from the library?

wverdese
  • 129
  • 1
  • 10
  • 1
    Error seems to be the same with this [SO thread](http://stackoverflow.com/questions/29343922/googleapiclient-is-throwing-googleapiclient-is-not-connected-yet-after-onconne) and was resolved. – ReyAnthonyRenacia Feb 01 '17 at 16:57
  • @noogui Thanks! Moving the client creation in `onCreate()` sounds more a workaround than a solution but I guess there's nothing more I can do. As it's said in this thread there's no way to report this bug to Google... – wverdese Feb 15 '17 at 11:01

1 Answers1

1

We need create googleAPIClient in onCreate() and sign out it in same activity.

Ex: In onCreate()

    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken(this.getString(R.string.default_web_client_id))
            .requestEmail()
            .requestProfile()
            .build();


    mGoogleApiClient = new GoogleApiClient.Builder(activity)
            .enableAutoManage(activity, new GoogleApiClient.OnConnectionFailedListener() {
                @Override
                public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
                }
            })
            .addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
                @Override
                public void onConnected(@Nullable Bundle bundle) {
                }

                @Override
                public void onConnectionSuspended(int i) {

                }
            })
            .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
            .build();

And Sign out it :

    Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(
            new ResultCallback<Status>() {
                @Override
                public void onResult(@NonNull Status status) {

                    if (mListener != null) {
                        mListener.onSigout("");
                    }
                }
            });

If we create mGoogleAPIClient with ApplicationContext, we can't sign out Google Account properly.

user2481102
  • 86
  • 1
  • 5