mac-man
mac-man

Reputation: 41

android google sign in window doesn't pop again after logout

On my Android app I have a Google Plus sign-in button in my MainActivity. I use these lines in my onCLick() method:

mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(Plus.API, Plus.PlusOptions.builder().build())
                .addScope(Plus.SCOPE_PLUS_LOGIN)
                .build();

mGoogleApiClient.connect();

And the first time I press the button a sign-in window comes up. I log in successfully with my google account into HomeActivity, and then successfully logout, using this 2 lines:

Plus.AccountApi.clearDefaultAccount(AppController.getInstance().getmGoogleApiClient());
mGoogleApiClient.disconnect();

(I tried to put mGoogleApiClient.connect(); as a 3rd line.. it didn't change anything)

So then I return to MainActivity, but when I press the sign-in Button again, it automatically signs me in back to HomeActivity, without popping up the sign-in window like the first time... although I alledgedly signed-out...

Does someone know what else should I add to my code? Or perhaps there is a way to manually show that sign-in window? What makes it pop the first time?

Thank you very much!

Upvotes: 0

Views: 1333

Answers (5)

TexD
TexD

Reputation: 262

Simple, This code work for me.

//for logout
    btnLogout.setOnClickListener(v -> {
        //this will revoke users access and logout
        GoogleSignInClient googleSignInClient = GoogleSignIn.getClient(HomeActivity.this,GoogleSignInOptions.DEFAULT_SIGN_IN);
        googleSignInClient.revokeAccess().addOnCompleteListener(HomeActivity.this,
                new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {
                        
                        // after this add signout also
                        FirebaseAuth.getInstance().signOut();
                        Toast.makeText(HomeActivity.this, "SignOut", Toast.LENGTH_SHORT).show();
                        //intent for login screen and finish this screen
                        Intent intent= new Intent(HomeActivity.this,MainActivity.class);
                        startActivity(intent);
                        finish();
                    }
                });
    });

Upvotes: 0

Abdul Rehman
Abdul Rehman

Reputation: 1

val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken(BuildConfig.WEB_CLIENT_ID)
            .requestEmail()
            .build()
        googleclient = GoogleSignIn.getClient(this, gso)
        googleclient.revokeAccess()

//i've faced also this problem and i figure out **revokeaccess()**method is popup all user account

Upvotes: 0

Surajkaran Meghwanshi
Surajkaran Meghwanshi

Reputation: 676

You have to add these lines on logout click code block

Firebase.auth.signOut()
mAuth?.signOut()
mGoogleSignInClient?.signOut()

this will resolve your problem.

Upvotes: 0

Borjan Spanovic
Borjan Spanovic

Reputation: 41

Add this part of code in your onCreate method:

    GoogleSignInOptions googleSignInOptions =
            new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                    .requestIdToken(getString(R.string.default_web_client_id))
                    .requestEmail()
                    .build();
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .enableAutoManage(this, connectionResult -> Toast.makeText(
                    this, "Connection failed",
                    Toast.LENGTH_SHORT).show())
            .addApi(Auth.GOOGLE_SIGN_IN_API, googleSignInOptions)
            .build();

And then simply add this line when signing out

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

Upvotes: 2

Himanshu Shekher Jha
Himanshu Shekher Jha

Reputation: 1344

You need to add the following code at Log out, so it will clear all user data from your app, and after re-login you will get popping up the sign-in window like the first time.

public void signOutFromGplus() {
    if (mGoogleApiClient.isConnected()) {
        Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
        Plus.AccountApi.revokeAccessAndDisconnect(mGoogleApiClient);
        mGoogleApiClient.disconnect();
        mGoogleApiClient.connect();
    }
}

Upvotes: -1

Related Questions