1

i just implemented Google signin into my app, but for some reason it does not activate the MainActivity when the user clicked the google Signin button.

This is the method which gets activated when the user click's on the signIn button:

private void signIn() {
    Intent signInIntent = mGoogleSignInClient.getSignInIntent();
    startActivityForResult(signInIntent, RC_SIGN_IN);
}

I have to add the activity somewhere which I want to activate. Normally I add it into the Intent. But when I do it like this:

private void signIn() {
    Intent signInIntent = mGoogleSignInClient.getSignInIntent();
    signInIntent.setClass(this, MainActivity.class); 
    startActivityForResult(signInIntent, RC_SIGN_IN);
}

Then the signin doesnt work anymore.

1 Answer 1

0

As you can see here, after calling the intent for the sign in you have to handle it overriding the onActivityResult method:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);

  // Result returned from launching the Intent from GoogleSignInClient.getSignInIntent(...);
  if (requestCode == RC_SIGN_IN) {
      // The Task returned from this call is always completed, no need to attach
      // a listener.
      Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
      handleSignInResult(task);
  }
}

then in your handleSignInResult you can check if the sign in was successful and than starting your activity

private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {
  try {
      GoogleSignInAccount account = completedTask.getResult(ApiException.class);

      // Signed in successfully, start your activity here.

  } catch (ApiException e) {
      // The ApiException status code indicates the detailed failure reason.
      // Please refer to the GoogleSignInStatusCodes class reference for more information.
      Log.w(TAG, "signInResult:failed code=" + e.getStatusCode());
      updateUI(null);
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.