Cheok Yan Cheng Cheok Yan Cheng - 4 months ago 145x
Android Question

How can I enforce GoogleApiClient to prompt account chooser UI each time I call connect?

Whenever I run the code for the very first time at my 1st app launched cycle

GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(context)
.addScope(Drive.SCOPE_APPFOLDER) // required for App Folder sample


I can see the following account chooser.

enter image description here

However, if previous connect is success, and I run the same code again for first time at my 2nd app launched cycle.

The account chooser will not pop up again.
is going to use the account name, I choose in previous app launch cycle.

I wish to have my account chooser pop up every-time.

I came across How to Clear GoogleApiClient Default Account and Credentials

The proposed solution doesn't work for my case.


If I had been connected for my previous app cycle, and I call the above code first time in my current app cycle, I will get the following exception.

java.lang.IllegalStateException: GoogleApiClient is not connected yet.
at Source)
at Source)

The following code won't work either.

if (mGoogleApiClient.isConnected()) {
// No chance to execute this code, if you run this code during app launch.
} else {
// No account chooser will pop up if you had been connected in previous app life cycle

May I know, how can I enforce GoogleApiClient to prompt account chooser UI each time I call connect?


In both GDAA and the REST Api, you have two options:
1/ you do not specify the account and the underlying system will manage it.
2/ you manage the account yourself.

If you use the first approach, you will never know who the user of your app selected. You can only 'clean' the account by means of clearDefaultAccountAndReconnect. The selection dialog pops up again and the user can select (add) another account.

If you need to know the current selected user account (i.e. for caching /persistence), you must manage the account selection yourself as you can see here (for REST) or here (for GDAA) - just follow the REQ_ACCPICK trail and the UT.AM class. This way you'll be in full control.

So, the short answer to your question is that you pop the

        null, new String[]{GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE}, true, null, null, null, null),

activity yourself and deliver the resulting

email = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME)

to your setAccountName(email) as in:

   GAC = new GoogleApiClient.Builder(act)

Good Luck