9

I'm using an AbstractAccountAuthenticator and I want to single account for my app. So when the user is opting to add a new account for this app I want to prompt a message. I saw other apps use a toast for the message, but for some reasons mine doesn't get displayed.

I display the message like this:

public Bundle addAccount() {
    if (accounts.size() > 0) {
        Toast.makeText(context, R.string.MSG_ONLY_ONE_ACCOUNT_IS_SUPPORTED, Toast.LENGTH_LONG).show();
        return null;
    }
}

Any ideas why? I'm checking the accounts number in the addAccount() method from AbstractAccountAuthenticator.

Bruno Flávio
  • 778
  • 10
  • 27
Alex
  • 191
  • 1
  • 10

1 Answers1

13

I've been looking around for the same. The following answers have helped me: 1, 2.

Using your code example:

private final Handler handler = new Handler();

public Bundle addAccount(...) {
    if (accounts.size() > 0) {
        final Bundle bundle = new Bundle();
        final String message = 
                  mContext.getString(R.string.MSG_ONLY_ONE_ACCOUNT_IS_SUPPORTED);
        bundle.putInt(AccountManager.KEY_ERROR_CODE, 1);
        bundle.putString(AccountManager.KEY_ERROR_MESSAGE, message);

        handler.post(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
            }
        });

        return bundle;
    }
}
Community
  • 1
  • 1
Bruno Flávio
  • 778
  • 10
  • 27