title: Google Sign In - ApiException 12501 when dismissing the sign in dialog
body: <p>I have implemented the Google Sign In successfully, it works fine and I get no exceptions when I actually go through the whole sign in process.</p>
  
  <p>But if I click on the Google sign in button and then hit the back button to dismiss the dialog, I somehow get the following error:</p>
  
  <blockquote>
    <p>Caused by: com.google.android.gms.tasks.RuntimeExecutionException: com.google.android.gms.common.api.ApiException: 12501: 
                          at com.google.android.gms.tasks.zzu.getResult(Unknown Source:17)</p>
  </blockquote>
  
  <p>which points me to this piece of code:</p>
  
  <pre><code>if (requestCode == RC_SIGN_IN) {
              val task = GoogleSignIn.getSignedInAccountFromIntent(data)
              try {
                  val account = task.result
                  firebaseAuthWithGoogle(account)
              } catch (e: ApiException) {
                  Toast.makeText(this, "Google Sign In Failed", Toast.LENGTH_SHORT).show()
              }
          }
  </code></pre>
  
  <p>specifically on the <code>val account = task.result</code> line.</p>
  
  <p>My question is, shouldn't it handle it by himself via the <code>catch</code> and print out the toast? Why is the whole app crashing instead?</p>
  
  <p>Here is my whole <code>SignInActivity</code>:</p>
  
  <pre><code>import android.content.Context
  import android.content.Intent
  import android.os.Bundle
  import android.support.design.widget.BottomSheetDialog
  import android.support.v7.app.AppCompatActivity
  import android.view.View
  import android.widget.Toast
  import com.dancam.subscriptions.ActiveSubscriptions.SubscriptionsMain
  import com.dancam.subscriptions.R.id.google_signin_button
  import com.dancam.subscriptions.R.id.signin_progressbar
  import com.google.android.gms.auth.api.signin.GoogleSignIn
  import com.google.android.gms.auth.api.signin.GoogleSignInAccount
  import com.google.android.gms.auth.api.signin.GoogleSignInClient
  import com.google.android.gms.auth.api.signin.GoogleSignInOptions
  import com.google.android.gms.common.SignInButton
  import com.google.android.gms.common.api.ApiException
  import com.google.firebase.auth.FirebaseAuth
  import com.google.firebase.auth.FirebaseUser
  import com.google.firebase.auth.GoogleAuthProvider
  import kotlinx.android.synthetic.main.reset_password_dialog.view.*
  import kotlinx.android.synthetic.main.sign_dialog.view.*
  import kotlinx.android.synthetic.main.signin.*
  import org.jetbrains.anko.defaultSharedPreferences
  import org.jetbrains.anko.sdk25.coroutines.onClick
  
  class SignInActivity: AppCompatActivity() {
      private var mAuth: FirebaseAuth? = null
      private var mGoogleSignInClient: GoogleSignInClient? = null
  
      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          setContentView(R.layout.signin)
  
          val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                  .requestIdToken(getString(R.string.default_web_client_id))
                  .requestEmail()
                  .build()
  
          mGoogleSignInClient = GoogleSignIn.getClient(this, gso)
          mAuth = FirebaseAuth.getInstance()
  
          google_signin_button.setSize(SignInButton.SIZE_WIDE)
          google_signin_button.setOnClickListener { signIn() }
      }
  
      override fun onStart() {
          super.onStart()
          val currentUser = mAuth?.currentUser
  
          if (currentUser!=null) loggedIn(currentUser)
      }
  
      private fun signIn() {
          val signInIntent = mGoogleSignInClient?.signInIntent
          startActivityForResult(signInIntent, RC_SIGN_IN)
          signin_progressbar.visibility = View.VISIBLE
      }
  
      override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
          super.onActivityResult(requestCode, resultCode, data)
  
          if (requestCode == RC_SIGN_IN) {
              val task = GoogleSignIn.getSignedInAccountFromIntent(data)
              try {
                  val account = task.result
                  firebaseAuthWithGoogle(account)
              } catch (e: ApiException) {
                  Toast.makeText(this, "Google Sign In Failed", Toast.LENGTH_SHORT).show()
              }
          }
      }
  
      private fun firebaseAuthWithGoogle(acct: GoogleSignInAccount) {
          val credential = GoogleAuthProvider.getCredential(acct.idToken, null)
          mAuth?.signInWithCredential(credential)
                  ?.addOnCompleteListener {
                      if (it.isSuccessful) {
                          signin_progressbar.visibility = View.GONE
                          val user = mAuth?.currentUser
                          loggedIn(user)
                      } else {
                          signin_progressbar.visibility = View.GONE
                          Toast.makeText(this, "Authentication Failed", Toast.LENGTH_SHORT).show()
                          loggedIn(null)
                      }
                  }
      }
  
      fun onSignUpClicked(args: View) {
          val view = layoutInflater.inflate(R.layout.sign_dialog, null)
          val dialog = BottomSheetDialog(this)
          dialog.setContentView(view)
          dialog.show()
  
          view.register_button.setOnClickListener {
              val email = view.signup_email.text.toString()
              val password = view.signup_password.text.toString()
              val password2 = view.signup_password2.text.toString()
  
              if (password == password2) {
                  mAuth?.createUserWithEmailAndPassword(email, password)
                          ?.addOnCompleteListener {
                              if (it.isSuccessful) {
                                  //sign in success updateUI
                                  val user = mAuth?.currentUser
                                  loggedIn(user)
                              } else {
                                  // signin failed
                                  //TODO: translate
                                  Toast.makeText(this, "Authentication Failed", Toast.LENGTH_SHORT).show()
                                  loggedIn(null)
                              }
                          }
              } else {
                  //TODO: translate
                  Toast.makeText(this, "Passwords do not match", Toast.LENGTH_LONG).show()
              }
          }
      }
  
      fun onSignInClicked(args: View) {
          val view = layoutInflater.inflate(R.layout.sign_dialog, null)
          val dialog = BottomSheetDialog(this)
  
          //TODO: translate
          view.dialog_textview.text = "Sign In"
          view.signup_password2.visibility = View.GONE
  
          dialog.setContentView(view)
          dialog.show()
  
          //TODO: translate
          view.register_button.text = "Sign In"
          view.register_button.setOnClickListener {
              val email = view.signup_email.text.toString()
              val password = view.signup_password.text.toString()
  
              mAuth?.signInWithEmailAndPassword(email, password)
                      ?.addOnCompleteListener {
                          if (it.isSuccessful) {
                              val user = mAuth?.currentUser
                              loggedIn(user)
                          } else {
                              //TODO: translate
                              Toast.makeText(this, "Sign In Failed", Toast.LENGTH_LONG).show()
                              view.forgotten_password.visibility = View.VISIBLE
  
                              view.forgotten_password.onClick {
                                  dialog.dismiss()
                                  resetDialog()
                              }
                          }
                      }
          }
      }
  
      private fun resetDialog() {
          val resetView = layoutInflater.inflate(R.layout.reset_password_dialog, null)
          val resetDialog = BottomSheetDialog(this)
          resetDialog.setContentView(resetView)
          resetDialog.show()
  
          resetView.send_reset_email.setOnClickListener {
              resetView.send_reset_email.isClickable = false
              val email = resetView.reset_email.text.toString()
              mAuth?.sendPasswordResetEmail(email)
                      ?.addOnSuccessListener {
                          //TODO: translate
                          Toast.makeText(this, "Email Sent", Toast.LENGTH_SHORT).show()
                          resetDialog.dismiss()
                      }
          }
      }
  
      private fun loggedIn(user: FirebaseUser?) {
          startActivity(Intent(this, SubscriptionsMain::class.java))
          finish()
      }
  
      fun onSkipClicked(args:View) {
          loggedIn(null)
      }
  
      companion object {
          const val RC_SIGN_IN = 0000    }
  }
  </code></pre>