Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Google Sign in Exceptions

I want to get the exceptions caused when a user fails to log into my app using Google Sign in. For example, in the handleSignInResult(@NonNull Task<GoogleSignInAccount> completedTask) method I use this:

private void handleSignInResult(@NonNull Task<GoogleSignInAccount> completedTask) {
    try {
        account = completedTask.getResult(ApiException.class);
        if(isSignedIn()) {
            Toast.makeText(this, "Success!", Toast.LENGTH_SHORT).show();
            startActivity(new Intent(MainActivity.this, Home.class));
        }
    } catch (ApiException e) {
            Toast.make(this, "Failed to login because: " + e.getCause(), Toast.LENGTH_LONG).show();
        }
    }
}

This helps me to get the cause of the error. But the most common cause is that when when no account is selected, this code returns null.

But I want to make a Toast when no account is selcted:

Toast.makeText(this, "Failed to login because: No account selected!", Toast.LENGTH_LONG).show();

I think that this can be achieved using the switch case but I don't know how to do that.

like image 539
Gourav Avatar asked Apr 27 '26 09:04

Gourav


1 Answers

This is what I should use:

private void handleSignInResult(@NonNull Task<GoogleSignInAccount> completedTask) {
    try {
        account = completedTask.getResult(ApiException.class);
        if(isSignedIn()) {
            Toasty.success(this, "Success!", Toast.LENGTH_SHORT, true).show();
            startActivity(new Intent(MainActivity.this, Home.class));
        }
    } catch (ApiException e) {
        if(e.getCause() != null) {
            Toast.makeText(this, "Failed to login because: " + e.getCause(), Toast.LENGTH_LONG).show();
        }
        else {
            Toast.makeText(this, "Failed to login because: No account Selected!", Toast.LENGTH_LONG).show();
        }
    }
}`
like image 153
Gourav Avatar answered Apr 29 '26 22:04

Gourav



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!