简体   繁体   中英

GoogleSignInClient.signOut() for Android does not sign out user

1) In my HomeActivity screen there will be a button for user sign out. This button leads to signOut() method of LoginActivity

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);

    //adding an onclicklistener to signOut button
    findViewById(R.id.sign_out_button).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            LoginActivity loginActivity = new LoginActivity();
            loginActivity.signOut();
        }
    });
}

2) In my LoginActivity , the signOut() method will perfom what is in this tutorial provided by Google

public void signOut() {
    mGoogleSignInClient.signOut()
            .addOnCompleteListener(this, new OnCompleteListener<Void>() {
                @Override
                public void onComplete(@NonNull Task<Void> task) {
                    Toast.makeText(getApplicationContext(), "Disconnecting...", Toast.LENGTH_LONG);
                    Intent i = new Intent(getApplicationContext(), LoginActivity.class);
                    startActivity(i);
                }
            });
}

I was hoping the code above would work but what happens is that when the intent to go back to LoginActivity is executed after sign out, the onStart() method below goes into if (account != null) and returns the user that was supposed to be logged out

@Override
public void onStart() {
    super.onStart();

    // Check for existing Google Sign In account
    GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);

    if (account != null) {
        findViewById(R.id.sign_in_button).setVisibility(View.GONE);
        Toast.makeText(this, "Logged as" + account.getDisplayName(), Toast.LENGTH_LONG).show();

    } else {
        findViewById(R.id.sign_in_button).setVisibility(View.VISIBLE);
    }
}

clearDefaultAccountAndReconnect() api will clear previously login account details. Try using below snippet in your signout onComplete() method in listener.

if (mGoogleApiClient.hasConnectedApi(Auth.GOOGLE_SIGN_IN_API)) {
        mGoogleApiClient.clearDefaultAccountAndReconnect();
        mGoogleApiClient.stopAutoManage(context);
        mGoogleApiClient.disconnect();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM