Android AccountPicker 'Add account' doesen't refresh the options

342 views Asked by At

When my app starts, I'd like to ask my users either to create an Account or to choose from existing ones. I've implemented an Authenticator (extended AccountAuthenticatorActivity, AbstractAccountAuthenticator, made a Service) It seems to be working, I can create new Accounts from Settings/Accounts.

When I start an AccountPicker, I get a list of already created Accounts. When I click Add acccount it shows up my Account creation Activity. But when I'm done with account creation, finishing that Activity, and going back to the AccountPicker I dont see a new option of the newly created Account. Although if I restart the app, the recently created Account is in the list.

How I start the AccountPicker:

Intent intent = accountManager.newChooseAccountIntent(null, null, new String[]{"test_namespace"}, null, null, null, null);
startActivityForResult(intent, TEST_CODE);

My questions:

  • Is it supposed to work like this?
  • Can I reload the content of the AccountPicker after I created a new Account?
  • Can I just simply return an Intent with the newly created Account when I return from my Account creation Activity?
1

There are 1 answers

0
kris larson On

In my authenticator activity, after the user authenticates on the server I check the existing accounts and explicitly add the account if it's not there:

    boolean accountRegistered = false;
    Account account = new Account(username, AccountAuthenticator.ACCOUNT_TYPE_MYAPP);
    AccountManager acctMgr = AccountManager.get(this);
    Account[] accounts = acctMgr.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE_MYAPP);
    for (Account acct : accounts) {
        if (acct.equals(account)) {
            accountRegistered = true;
            break;
        }
    }

    if (accountRegistered) {
        acctMgr.setPassword(account, password);
    } else {
        acctMgr.addAccountExplicitly(account, password, null);
    }

After I do this, I see the account in the account picker.

I can't guarantee this is 100% correct; with the undocumented authentication classes, we're all flying blind.