0

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();
            }

        }
    }
}
Owen Chak
  • 51
  • 7
  • Hi Owen Chak, i am also facing same issue. First time email selection prompt is opening then saving selected email using mCredential.setSelectedAccountName(accountName); method, after trying add calendar event, mCredential.getSelectedAccountName() is returning null. – Ravikumar11 Mar 23 '17 at 05:43
  • Seems that it was already answered: [similar question](https://stackoverflow.com/questions/33085685/) – Gregory Buiko Jul 17 '17 at 22:36

0 Answers0