I'm implementing an authentication system in Android 4.2 using the authentication sample as an example. In my application I have a MenuActivity with the Login option. If that option is clicked and the user isn't logged in already, my authentication system starts and the AuthenticationActivity is showed.
When the login ends I want to execute some code in the MenuActivity so it knows the user has logged in. I created a callback to do so, but it's never called. The login works fine, if I close the app and start it again it detects the user as logged in.
In my Menu Activity I have this:
public void login() {
if(mAccount != null)
Toast.makeText(MenuActivity.this, getString(R.string.account_exists), Toast.LENGTH_LONG).show();
else{
mAccountManager.addAccount(ACCOUNT_TYPE, AUTHTOKEN_TYPE, null, null, this, completeCallbackLogin, null);
}
}
// Callback called when the login ends.
private AccountManagerCallback<Bundle> completeCallbackLogin = new AccountManagerCallback<Bundle>() {
public void run(AccountManagerFuture<Bundle> arg0) {
Log.d("MenuActivity", "CALLBACK");
// When the login ends we save the account in the global variables
refreshAccount();
Toast.makeText(MenuActivity.this, getString(R.string.login_ok), Toast.LENGTH_LONG).show();
}
};
In my AuthenticatorActivity (called LoginActivity) I do the following:
Account account = new Account(mUsername, ACCOUNT_TYPE);
try{
mAccountManager.addAccountExplicitly(account, mPassword, newBundle());
}catch(Exception e){
e.printStackTrace();
return;
}
final Intent intent = new Intent();
intent.putExtra(AccountManager.KEY_ACCOUNT_NAME, mUsername);
intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE, ACCOUNT_TYPE);
intent.putExtra(AccountManager.KEY_PASSWORD, mPassword);
intent.putExtra(AccountManager.KEY_AUTHTOKEN, mAuthToken);
intent.putExtra(AccountManager.KEY_BOOLEAN_RESULT, true);
LoginActivity.this.setAccountAuthenticatorResult(intent.getExtras());
setResult(RESULT_OK, intent);
LoginActivity.this.finish();
Like I said, my callback is never called. Why is that?
[EDIT]
I just noticed that if I run the application again (Eclipse->Run) the callback is called right before my application closes to be installed again.
[/EDIT]
In my AbstractAccountAuthenticator I forgot to add the following line in the addAccount method: