- Constructor must be invoked in OnCreate() method of activity, otherwise you'll get exception like
java.lang.IllegalStateException: LifecycleOwner is attempting to register while current state is RESUMED.
It should look like this:
GoogleAccountPicker googleAccountPicker = new GoogleAccountPicker(this);
- Use pickAccount() method anywhere to pick an account. It doesn't return anything, because choosing an account is non blocking operation. You have to do it like this:
googleAccountPicker.pickAccount(account -> {
System.out.println(account.getName());
});
or, if you don't want to use lambdas:
googleAccountPicker.pickAccount(new OnAccountPickedListener() {
public void onAccountPicked(Account account) {
System.out.println(account.getName());
}
});
- You can also use getAccounts() method to get all available accounts without showing up dialog. It's useful when your app remembers once chosen account.
Remember to set up permission in manifest:
<b><uses-permission android:name="android.permission.GET_ACCOUNTS"/></b>
and add dependency to build.gradle:
<b>implementation 'com.google.android.gms:play-services-base:17.6.0'</b>