Aritra Roy Aritra Roy - 3 years ago 240
Android Question

Google Plus Sign In Account Selection Dialog Issue

BACKGROUND

I am using Google Plus OAuth in my app along with Firebase. When the user taps the sign in button, and account selection dialog appears and the user selects the particular account of choice and signs in.

PROBLEM

Suppose the user has two accounts in that device, A and B. Initially, he selects account A to sign in and then uses the app and then decides to sign out. Now he clicks the sign in button again, but the account selection dialog doesn't appear and it automatically signs in using the previously selected account A.

WHAT I WANT

I want the user to be presented with the account selection dialog every time and give them a chance to select a different account if they want to.

CODE

Here is a small code snippet of how I am using the Google Api Client for sign in.

String mClientId = parcel.getProviderExtra().getString(CLIENT_ID_KEY);
GoogleSignInOptions googleSignInOptions;

googleSignInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(mClientId)
.requestEmail()
.build();

mGoogleApiClient = new GoogleApiClient.Builder(activity)
.addApi(Auth.GOOGLE_SIGN_IN_API, googleSignInOptions)
.build();

mGoogleApiClient.connect();


and sign out,

Auth.GoogleSignInApi.signOut(mGoogleApiClient);


Any help will be highly appreciated.

Answer Source

Got this resolved by clearing the default account every time before sign in.

@Override
    public void startLogin(Activity activity, String mEmail) {
        Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
        /**
         * Clearing default account every time so that the account picker dialog can be enforced
         */
        if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
            mGoogleApiClient.clearDefaultAccountAndReconnect();
        }
        activity.startActivityForResult(signInIntent, RC_SIGN_IN);
    }

This will enforce the dialog to appear every time.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download