0

I am trying to implement Google login and and logout for my app. The logout seems to be giving the following error; java.lang.RuntimeException: Unable to start activity ComponentInfo: java.lang.IllegalStateException: GoogleApiClient is not connected yet.

LogoutActivity:

public class LogoutActivity_new extends AppCompatActivity implements
    GoogleApiClient.OnConnectionFailedListener{
private GoogleApiClient mGoogleApiClient;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
   // SocialLogin.signOut();

   // GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN);

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
           .addApi(Auth.GOOGLE_SIGN_IN_API)
            .build();
    signOut();

}

private void signOut() {
    Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(
            new ResultCallback<Status>() {
                @Override
                public void onResult(Status status) {
                    // [START_EXCLUDE]
                    //updateUI(false);
                    // [END_EXCLUDE]
                }
            });
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {

}
}

Pls can someone help

Khadija Daruwala
  • 1,185
  • 3
  • 25
  • 54

2 Answers2

0

You probably need to call the connect method on your mGoogleApiClient first and wait until it connects before calling signOut. Good place to do so is onResume method

x0r
  • 1,271
  • 11
  • 13
0

You cannot call signout() just after creating mGoogleApiClient, as You don't know if client is already connected(which is nearly impossible, as a matter of code execution speed).

You can use this method, when You know that client already connected itself to googleApi, for examaple, when onConnected() is called.

Example:

@Override
void OnConnected(Bundle bundle)
{
    signout();
}

OnConnected() is called by googleApi when client connects itself.

You can also set some boolean field like 'connected' on true in OnConnected method, and then you can check it in other places in code to call signout() like:

if(connected) signout();
bartexsz
  • 239
  • 1
  • 11