Reputation: 174
In my android application I use account manager. If there are multiple accounts, I ask to the user to select one account using accountManager.newChooseAccountIntent. How can I remember this selection for the next time in login form and other activities?
Upvotes: 0
Views: 258
Reputation: 1056
Here you go:
1. Launch the intent to show account selection dialog to the user-
public static final int CHOOSE_ACCOUNT_REQUEST_CODE = 0;
private String mEmail;
Intent intent = AccountManager.newChooseAccountIntent(null, null,
new String[] { acc_type }, true, null, null, null, null);
startActivityForResult(intent, CHOOSE_ACCOUNT_REQUEST_CODE);
2. Get the result inside onActivityResult -
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent
data) {
if (requestCode == REQUEST_CODE_PICK_ACCOUNT) {
if (resultCode == RESULT_OK) {
mEmail = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
} else if (resultCode == RESULT_CANCELED) {
// The account picker dialog closed without selecting an account.
// Notify users that they must pick an account to proceed.
}
}
}
3. You can then store the mEmail in Shared Preference to access within the application:
SharedPreferences sharedPreferences =
context.getSharedPreferences("app_pref", MODE_PRIVATE);
editor = sharedPreferences.edit();
editor.putBoolean("Email", mEmail);
editor.commit();
4. To access the stored email from shared preference:
String email = sharedPreferences.getString("Email", "Use a default value");
Upvotes: 2