Parth Patel Parth Patel - 5 months ago 18
Android Question

"GoogleApiClient is not connected yet" on logout when using Firebase auth with google sign in

I'm using Firebase auth with Google Sign In but I want to signout from another activity but when I logout using this method which works perfectly from the same activity but not with another activity. Here is the method.

public void logOut() {
mAuth.signOut();
// Google sign out
Auth.GoogleSignInApi.signOut(googleApiClient).setResultCallback(
new ResultCallback<Status>() {
@Override
public void onResult(@NonNull Status status) {
authorizeUser(null);
}
});
}


But when making this method static and the googleApiClient static it still doesn't work and when i perform only

FirebaseAuth.getInstance().signOut();


the error i m getting is this (logcat)

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.igov, PID: 21316
java.lang.IllegalStateException: GoogleApiClient is not connected yet.
at com.google.android.gms.internal.zzoe.zzd(Unknown Source)
at com.google.android.gms.internal.zzoh.zzd(Unknown Source)
at com.google.android.gms.internal.zzof.zzd(Unknown Source)
at com.google.android.gms.auth.api.signin.internal.zzc.signOut(Unknown Source)
at com.igov.design.LoginActivity.logOut(LoginActivity.java:159)
at com.igov.design.LoginActivity$2.onClick(LoginActivity.java:62)
at android.view.View.performClick(View.java:5198)
at com.igov.design.MainActivity.onNavigationItemSelected(MainActivity.java:101)
at android.support.design.widget.NavigationView$1.onMenuItemSelected(NavigationView.java:152)
at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:810)
at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:957)
at android.support.design.internal.NavigationMenuPresenter$1.onClick(NavigationMenuPresenter.java:318)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Answer

Register a Connection Callback to Google Api Client

Classes Used:

  • LoginActivity class (Activity to Login)
  • LogoutActivity class (Activity to Logout)

Code:

    public void logout() {
       mGoogleApiClient.connect();
       mGoogleApiClient.registerConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
        @Override
        public void onConnected(@Nullable Bundle bundle) {

            FirebaseAuth.getInstance().signOut();
            if(mGoogleApiClient.isConnected()) {
                Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(new ResultCallback<Status>() {
                    @Override
                    public void onResult(@NonNull Status status) {
                        if (status.isSuccess()) {
                            Log.d(TAG, "User Logged out");
                            Intent intent = new Intent(LogoutActivity.this, LoginActivity.class);
                            startActivity(intent);
                            finish();
                        }
                    }
                });
            }
        }

        @Override
        public void onConnectionSuspended(int i) {
            Log.d(TAG, "Google API Client Connection Suspended");
        }
    });
}
Comments