2

I'm using Google+ sign in on my application, and followed the reference such as the Getting Started and Google+ Sign-in for Android.

So the situation is the following:
I have one LoginActivity and a MainActivity, both extend on BaseActivity (so they can share the instance of GoogleApiClient, and the necessary interfaces implementations) and when I sign in, the LoginActivity does the following:

    public void onConnected(Bundle connectionHint) {
        super.onConnected(connectionHint);
        mSignInClicked = false;
        launchMain();
    }
    private void launchMain() {
        Intent i = new Intent(this, MainActivity.class);
        startActivity(i);
        finish();
    }

And the MainActivity will launch, great!

But when I want to sign out from the MainActivity I do the following:

protected static GoogleApiClient mGoogleApiClient;
public void signOut() {
    if (mGoogleApiClient.isConnected()) {
        Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
        mGoogleApiClient.disconnect();
        mGoogleApiClient.connect();
        Intent i = new Intent(getApplicationContext(),LoginActivity.class);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(i);
    }
}

The mGoogleApiClient.isConnected() check is always false, and I'm never able to disconnect.

Besides that I'm confused why should I do the connect right after the disconnect.

Tiago Martins
  • 31
  • 1
  • 5

1 Answers1

1

So I'll be answering my own question:

On the BaseActivity I was doing this:

protected void onStop() {
    super.onStop();

    if (mGoogleApiClient.isConnected()) {
        mGoogleApiClient.disconnect();
    }
}

Meaning that when I tried to disconnect, it was already disconnected.
The main thing here is to follow the rules in this answer and in my situation the rule is this:

Implement mostly in a baseactivity, and have the others extend that. This is connect/disconnect in each activity, but with code in only one place.

Community
  • 1
  • 1
Tiago Martins
  • 31
  • 1
  • 5