I'm working on an Android app that uses the AccountManager to store accounts & credentials. One problem I've been having is that even though I pass in a bunch of String values to the AccountManager's addAccountExplicitly data bundle.
I have checked to make sure that the bundle is not empty and that it contains the values before adding the account. And most of the time this is the case, but every once in a while I get null when I query these values.
The null return values are more common after I've just deleted the account and re-added it.
I'm doing my query inside the onAccountsUpdated method of an OnAccountsUpdateListener implementation, so the account should be added and good to go, right?
Thanks for any help
AuthenticatorActivity
// in the AuthenticatorActivity
Bundle userData = new Bundle();
userData.put (k1, v1);
userData.put (k2, v2);
userData.put (k3, v3);
userData.put (k4, v4);
userData.put (k1, v1);
Account a = new Account ("acc name", "com.account.type");
AccountManager am = AccountManager.get(this);
OnAccountsUpdateListener listener = new OnAccountsUpdateListener() {
@Override
public void onAccountsUpdated(Account[] accounts) {
Account mine = findAccount(accounts, account); // match account name
notifySignedIn(mine); // tell the world you're signed in
am.removeOnAccountsUpdatedListener(this);
}
};
am.addOnAccountsUpdatedListener(listener, handler, false);
am.addAccountExplicitly(a, "themostsecurepwintheworld", userData);
Some other thread
AccountManager am = AccountManager.get(mContext);
final string value2 = am.getUserData(mAccount, k2);
if (TextUtils.isEmpty(value2)) {
Log.d("WTF", "value is empty");
}
I am aware of one bug in Honeycomb plus that can cause your issue. If you try to getUserData of the account before it is registered all subsequent getUserData calls will return null.
If you look at the code. AccountManager has an in-memory cache that is backed by a sqlite database. Calling getUserData populates the in-memory cache userdata, even if it is not registered. If it is not registered it interprets that as no userdata. Registering an account only populates the database and does not invalidate the in-memory store.
This may be causing your issue.
The workaround is to remove the account before calling addAccountExplicitly.
Ya this issue sucks and is a HUGE bug in AccountManger IMO and allows third parties to essence DOS your app.