Android – Google login process with multiple activities
I have an application with three activities: login activity, main activity and detailed activity
The login activity uses Google login to obtain the user's account (token ID and e-mail). Both the main activity and the detail activity require Google signinaccount
I don't want the application to always start when the login activity is active and to log back in transparently when the user restarts the application
I don't want to store the token ID and user email in shared preferences. I want to operate agoglesigninaccount directly anywhere in my application
What is the correct process for handling this check-in scheme? Should I try to execute silent signin on each screen, or is there a better way to store and retrieve Google signinaccount?
resolvent:
Yes, you can use silentsignin
If you are looking for Google signinaccount on the UI thread, the following is the asynchronous version:
Auth.GoogleSignInApi.silentSignIn(mGoogleApiClient).setResultCallback(
new ResultCallback<GoogleSignInResult>() {
@Override
public void onResult(@NonNull GoogleSignInResult result) {
if (googleSignInResult.isSuccess() {
GoogleSignInAccount gsa = googleSignInResult.getSignInAccount();
}
}
});
Note: if your mgogleapiclient is not built using enableautomanage, you must manually call mgogleapiclient. Connect() and mgogleapiclient. Disconnect() for the above code example to work properly. For more details, see more information about managing the googleapiclient connection lifecycle
Alternatively, on a non UI thread, you can use the following example code:
try {
ConnectionResult result = mGoogleApiClient.blockingConnect();
if (result.isSuccess()) {
GoogleSignInResult googleSignInResult =
Auth.GoogleSignInApi.silentSignIn(googleApiClient).await();
if (googleSignInResult.isSuccess() {
GoogleSignInAccount gsa = googleSignInResult.getSignInAccount();
}
}
} finally {
mGoogleApiClient.disconnect();
}
Please check documentation for more details, including back - end authentication or API calls using Google account ID tokens