I am trying to ask users to select their google account here. After I use the "setSelectedAccoutName" method, i try to print out the selected account name which returns null. I am 100% sure that accountName is not null. So does anyone know why mCredential.getSelectedAccountName is null?
private void pickContact() {
AccountManager accountManager = AccountManager.get(getActivity().getApplicationContext());
Account[] accounts = accountManager.getAccountsByType(null);
Intent intent = AccountPicker.newChooseAccountIntent(null, null,
new String[]{"com.google"},
false, null, null, null, null);
startActivityForResult(intent, 23);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
System.out.println("hi");
if (requestCode == 23) {
if (resultCode == Activity.RESULT_OK) {
Bundle bundle = data.getExtras();
String accountName = bundle.getString(AccountManager.KEY_ACCOUNT_NAME);
System.out.println(accountName);
if (accountName != null) {
mCredential.setSelectedAccountName(accountName);
System.out.println(mCredential.getSelectedAccountName());
getResultsFromApi();
} else {
pickContact();
}
}
}
}