I'm trying to determine if the user already added the account, to prevent adding the same account multiple times on my app. I'm using my own account authenticator activity. Here is my code:
public class AuthenticatorActivity extends AccountAuthenticatorActivity {
private AccountManager mAccountManager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.setup_mail_dialog);
mAccountManager = AccountManager.get(getBaseContext());
findViewById(R.id.bSetupConfirm).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkAccount();
}
});
}
private void checkAccount() {
mAccountManager = AccountManager.get(getBaseContext());
String accountName = ((EditText) findViewById(R.id.etSetupEmail)).getText().toString().trim();
Account[] accounts = mAccountManager.getAccountsByType(getResources().getString(R.string.account_type));
for(Account account : accounts) {
if(account.name == accountName) {
System.out.println("Account exists");
return;
}else{
System.out.println("Account do not exists");
return;
}
}
}
However even if the account exists the app always show the "Account do not exists".
By references you must compare it with an equals.
The Solution:
if(account.name.equals(accountName))