Skip to content Skip to sidebar Skip to footer

Android Accountmanager.getuserdata() Returns Null

I have a similar problem like this AccountManager getUserData returning null despite it being set But the solutions did not work for me My Authenticator.java public class Authenti

Solution 1:

There is a much simpler solution:

Don't add the user-data with addAccountExplicitly(Account, String, Bundle). Instead, just pass an empty bundle and use setUserData(Account, String, String) to add the user-data after you created the account with addAccountExplicitly.

It may be a little less convenient, but it will make sure that the user-data cache is populated properly and won't return null.

Solution 2:

After some testing I've found these issues.

  • The problem only occurs on the account created after an account was manually deleted through accounts in Settings.
  • Restarting the device or reinstalling the app fixes the issue (until the first point happens again).
  • So far I have only seen the issue on a HTC

The solution:

Call the below method in addAccount() of your Authenticator.

/**
 * This method is a hack to fix a bug that is found on HTC devices.
 *
 * The issue is basically when ever an account is manually deleted through the devices
 * settings the user data that is saved in the next account that is created is not accessible.
 * The solution is to create a temp account and delete it from the app instead. Deleting
 * accounts via the AccountManager gets around the bug.
 */privatevoidhtcAccountSyncHack() {
    ContextappContext= [Application].getInstance();
    Accountaccount=newAccount([accountName], [accountType]);
    AccountManageraccountManager= (AccountManager) appContext.getSystemService(
            Context.ACCOUNT_SERVICE);
    accountManager.addAccountExplicitly(account, null, null);
    accountManager.removeAccount(account, null, null);
    SharedprefsMgr.setBooleanOnSessionSets(SharedprefsMgr.ACCOUNT_MANUALLY_DELETED, false);
}

Ideally you will have a ContentProvider that is registered as a OnAccountsUpdateListener in AccountManager. You can then use this to work out if the account was manually deleted or not. If it wasn't there is no need to call this method every time.

Solution 3:

Marten's answer did not resolve this in our app. The documentation says "It is safe to call this method from the main thread" and our app was calling AccountManager.getUserData() on a background thread, which usually worked fine, but apparently isn't guaranteed to work.

Post a Comment for "Android Accountmanager.getuserdata() Returns Null"