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 Authenticator extends AbstractAccountAuthenticator{
private Context context;
public Authenticator(Context context) {
super(context);
this.context = context;
}
@Override
public Bundle editProperties(AccountAuthenticatorResponse response,
String accountType) {
// TODO Auto-generated method stub
return null;
}
@Override
public Bundle addAccount(AccountAuthenticatorResponse response,
String accountType, String authTokenType,
String[] requiredFeatures, Bundle options)
throws NetworkErrorException {
Bundle result = new Bundle();
Intent intent = new Intent(context,LoginActivity.class);
intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE, accountType);
intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
result.putParcelable(AccountManager.KEY_INTENT, intent);
return result;
}
@Override
public Bundle confirmCredentials(AccountAuthenticatorResponse response,
Account account, Bundle options) throws NetworkErrorException {
// TODO Auto-generated method stub
return null;
}
@Override
public Bundle getAuthToken(AccountAuthenticatorResponse response,
Account account, String authTokenType, Bundle options)
throws NetworkErrorException {
return null;
}
@Override
public String getAuthTokenLabel(String authTokenType) {
// TODO Auto-generated method stub
return null;
}
@Override
public Bundle updateCredentials(AccountAuthenticatorResponse response,
Account account, String authTokenType, Bundle options)
throws NetworkErrorException {
// TODO Auto-generated method stub
return null;
}
@Override
public Bundle hasFeatures(AccountAuthenticatorResponse response,
Account account, String[] features) throws NetworkErrorException {
return null;
}
}
I added an account like this
mAccount = new Account("SalesGenie", "com.ambertag.salesgenie.ACCOUNT");
mAccountManager.addAccountExplicitly(mAccount, password, userData);
But when I try to get user data by calling getUserData()
I get null
There is a much simpler solution:
Don't add the user-data with
addAccountExplicitly(Account, String, Bundle)
. Instead, just pass an empty bundle and usesetUserData(Account, String, String)
to add the user-data after you created the account withaddAccountExplicitly
.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
.After some testing I've found these issues.
The solution:
Call the below method in addAccount() of your Authenticator.
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.