title: Android login activity only shows Google sign-in upon launch
body: <p>The first time opening a newly installed version of my app shows a custom login/register field and a Google sign-in button beneath the custom option. After logging in, I am brought to the MainActivity without an issue, however, when I try to re-launch the app, it always prompts me with the Google sign-in that I cannot get past without either logging in with a Google account or uninstalling and reinstalling the app.</p>
  
  <p><strong>LoginActivity.java</strong></p>
  
  <pre><code>public class LoginActivity extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener, OnClickListener {
  
  
  private static final String TAG = "LoginActivity";
  private static final int RC_SIGN_IN = 9001;
  
      // Id to identity READ_CONTACTS permission request.
  private static final int REQUEST_READ_CONTACTS = 0;
  
  // UI references.
  private EditText mEmailView;
  private EditText mPasswordView;
  private View mProgressView;
  private View mLoginFormView;
  
  private Button mEmailSignInButton;
  private Button mEmailRegisterButton;
  private SignInButton googleSignInButton;
  
  // Firebase Auth
  private FirebaseAuth fbAuth;
  private FirebaseUser fbu;
  
  // Firebase DB
  private FirebaseDatabase database;
  private DatabaseReference myUsers;
  
  // Google
  private GoogleApiClient mGoogleApiClient;
  private GoogleSignInOptions gso;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_login);
      // Set up the login form.
      mEmailView = (EditText) findViewById(R.id.email_edittext);
      mPasswordView = (EditText) findViewById(R.id.password_edittext);
  
      googleSignInButton= (SignInButton) findViewById(R.id.google_sign_in_button);
      googleSignInButton.setSize(SignInButton.SIZE_STANDARD);
      googleSignInButton.setOnClickListener(this);
  
      mPasswordView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
          @Override
          public boolean onEditorAction(TextView textView, int id, KeyEvent keyEvent) {
              if (id == R.id.login || id == EditorInfo.IME_NULL) {
                  attemptEmailLogin();
                  return true;
              }
              return false;
          }
      });
  
      mEmailRegisterButton = (Button) findViewById(R.id.register_button);
      mEmailRegisterButton.setOnClickListener(new OnClickListener() {
          @Override
          public void onClick(View view) {
              attemptEmailRegister();
          }
      });
  
      mEmailSignInButton = (Button) findViewById(R.id.sign_in_button);
      mEmailSignInButton.setOnClickListener(new OnClickListener() {
          @Override
          public void onClick(View view) {
              attemptEmailLogin();
          }
      });
  
      mLoginFormView = findViewById(R.id.login_form);
      mProgressView = findViewById(R.id.login_progress);
  
      // Google sign in
      gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
              .requestIdToken(getString(R.string.default_web_client_id))
              .requestEmail()
              .build();
      buildGoogleApiClient();
  
      // Initialize FirebaseAuth instance
      fbAuth = FirebaseAuth.getInstance();
      fbu = fbAuth.getCurrentUser();
      if (fbu != null) {
          Intent signInIntent = Auth
                  .GoogleSignInApi
                  .getSignInIntent(mGoogleApiClient);
          startActivityForResult(signInIntent, RC_SIGN_IN);
          finish();
      } else {
          Log.d(TAG, "No user is logged in");
      }
  
      // FireBase DB
      database = FirebaseDatabase.getInstance();
      myUsers = database.getReference("users");
  }
  
  @Override
  protected void onStart() {
      super.onStart();
      fbu = fbAuth.getCurrentUser();
      if (fbu != null) {
          Intent signInIntent = Auth
                  .GoogleSignInApi
                  .getSignInIntent(mGoogleApiClient);
          startActivityForResult(signInIntent, RC_SIGN_IN);
          finish();
      } else {
          Log.d(TAG, "No user is logged in");
      }
  }
  
  protected synchronized void buildGoogleApiClient() {
      Log.i(TAG, "Building GoogleApiClient");
      mGoogleApiClient = new GoogleApiClient.Builder(this)
              .enableAutoManage(this /* FragmentActivity (AppCompatActivity) */, this /* OnConnectionFailedListener */)
              .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
              .build();
      mGoogleApiClient.connect();
  }
  
  private void attemptEmailRegister() {
  
      // Reset errors.
      mEmailView.setError(null);
      mPasswordView.setError(null);
  
      // Store values at the time of the login attempt.
      String email = mEmailView.getText().toString();
      String password = mPasswordView.getText().toString();
  
      boolean cancel = false;
      View focusView = null;
  
      // Check for a valid password, if the user entered one.
      if (!TextUtils.isEmpty(password) &amp;&amp; !isPasswordValid(password)) {
          mPasswordView.setError(getString(R.string.error_invalid_password));
          focusView = mPasswordView;
          cancel = true;
      }
  
      // Check for a valid email address.
      if (TextUtils.isEmpty(email)) {
          mEmailView.setError(getString(R.string.error_field_required));
          focusView = mEmailView;
          cancel = true;
      } else if (!isEmailValid(email)) {
          mEmailView.setError(getString(R.string.error_invalid_email));
          focusView = mEmailView;
          cancel = true;
      }
  
      if (cancel) {
          // There was an error; don't attempt register and focus the first
          // form field with an error.
          focusView.requestFocus();
      } else {
          // perform the user register attempt.
          showProgress(true);
          fbAuth.createUserWithEmailAndPassword(email, password)
                  .addOnCompleteListener(this, new OnCompleteListener&lt;AuthResult&gt;() {
                      @Override
                      public void onComplete(@NonNull Task&lt;AuthResult&gt; task) {
                          if (task.isSuccessful()) {
                              // Register success
                              Log.d(TAG, "createUserWithEmail:success");
                              FirebaseUser user = fbAuth.getCurrentUser();
                              onAuthSuccess(user);
                          } else {
                              // If register fails, display a message to the user.
                              Log.w(TAG, "createUserWithEmail:failure", task.getException());
                              Toast.makeText(LoginActivity.this, "Registration authentication failed.",
                                      Toast.LENGTH_SHORT).show();
                              Intent startMainActivity = new Intent(LoginActivity.this, LoginActivity.class);
                              startActivity(startMainActivity);
                              finish();
                          }
                      }
                  });
      }
  
  
  }
  
  private void attemptEmailLogin(){
  
      // Reset errors.
      mEmailView.setError(null);
      mPasswordView.setError(null);
  
      // Store values at the time of the login attempt.
      String email = mEmailView.getText().toString();
      String password = mPasswordView.getText().toString();
  
      boolean cancel = false;
      View focusView = null;
  
      // Check for a valid password, if the user entered one.
      if (!TextUtils.isEmpty(password) &amp;&amp; !isPasswordValid(password)) {
          mPasswordView.setError(getString(R.string.error_invalid_password));
          focusView = mPasswordView;
          cancel = true;
      }
  
      // Check for a valid email address.
      if (TextUtils.isEmpty(email)) {
          mEmailView.setError(getString(R.string.error_field_required));
          focusView = mEmailView;
          cancel = true;
      } else if (!isEmailValid(email)) {
          mEmailView.setError(getString(R.string.error_invalid_email));
          focusView = mEmailView;
          cancel = true;
      }
  
      if (cancel) {
          // There was an error; don't attempt register and focus the first
          // form field with an error.
          focusView.requestFocus();
      } else {
          // perform the user register attempt.
          showProgress(true);
          fbAuth.signInWithEmailAndPassword(email, password)
                  .addOnCompleteListener(this, new OnCompleteListener&lt;AuthResult&gt;() {
                      @Override
                      public void onComplete(@NonNull Task&lt;AuthResult&gt; task) {
                          if (task.isSuccessful()) {
                              // Sign in success
                              Log.d(TAG, "signInWithEmail:success");
                              FirebaseUser user = fbAuth.getCurrentUser();
                              Intent startMainActivity = new Intent(LoginActivity.this, MainActivity.class);
                              startActivity(startMainActivity);
                          } else {
                              // If sign in fails, display a message to the user.
                              Log.w(TAG, "signInWithEmail:failure", task.getException());
                              Toast.makeText(LoginActivity.this, "Login authentication failed.",
                                      Toast.LENGTH_SHORT).show();
                              Intent startMainActivity = new Intent(LoginActivity.this, LoginActivity.class);
                              startActivity(startMainActivity);
                          }
  
                      }
                  });
      }
  
  
  }
  
  private void onAuthSuccess(FirebaseUser user) {
      String username = usernameFromEmail(user.getEmail());
      String[] names = firstAndLastNameFromDisplayName(user.getDisplayName());
  
      // Write new user
      writeNewUser(user.getUid(), names, username, user.getEmail());
  
      // Go to MainActivity
      Intent startMainActivity = new Intent(LoginActivity.this, MainActivity.class);
      startActivity(startMainActivity);
      finish();
  }
  private String usernameFromEmail(String email) {
      if (email.contains("@")) {
          return email.split("@")[0];
      } else {
          return email;
      }
  }
  private String[] firstAndLastNameFromDisplayName(String fullName) {
      if (fullName != null) {
          if (fullName.contains(" ")) {
              return new String[]{fullName.split(" ")[0], fullName.split(" ")[1]};
          } else {
              return new String[]{fullName, "emptyLastName"};
          }
      } else {
          return new String[]{"defaultfirst","defaultlast"};
      }
  }
  private void writeNewUser(String userId, String[] names, String username, String email) {
      User user = new User(username,names[0],names[1], email);
      myUsers.child("users").child(userId).setValue(user);
  }
  
  private boolean isEmailValid(String email) {
      return email.contains("@");
  }
  
  private boolean isPasswordValid(String password) {
      return (password.length() &gt; 4) &amp;&amp; !password.contains("\\");
  }
  
  /**
   * Shows the progress UI and hides the login form.
   */
  @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
  private void showProgress(final boolean show) {
      // On Honeycomb MR2 we have the ViewPropertyAnimator APIs, which allow
      // for very easy animations. If available, use these APIs to fade-in
      // the progress spinner.
      if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.HONEYCOMB_MR2) {
          int shortAnimTime = getResources().getInteger(android.R.integer.config_shortAnimTime);
  
          mLoginFormView.setVisibility(show ? View.GONE : View.VISIBLE);
          mLoginFormView.animate().setDuration(shortAnimTime).alpha(
                  show ? 0 : 1).setListener(new AnimatorListenerAdapter() {
              @Override
              public void onAnimationEnd(Animator animation) {
                  mLoginFormView.setVisibility(show ? View.GONE : View.VISIBLE);
              }
          });
  
          mProgressView.setVisibility(show ? View.VISIBLE : View.GONE);
          mProgressView.animate().setDuration(shortAnimTime).alpha(
                  show ? 1 : 0).setListener(new AnimatorListenerAdapter() {
              @Override
              public void onAnimationEnd(Animator animation) {
                  mProgressView.setVisibility(show ? View.VISIBLE : View.GONE);
              }
          });
      } else {
          // The ViewPropertyAnimator APIs are not available, so simply show
          // and hide the relevant UI components.
          mProgressView.setVisibility(show ? View.VISIBLE : View.GONE);
          mLoginFormView.setVisibility(show ? View.GONE : View.VISIBLE);
      }
  }
  
  @Override
  public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
      Log.e(TAG, "onConnectionFailed called");
  }
  
  @Override
  public void onClick(View view) {
      switch (view.getId()) {
          case R.id.google_sign_in_button:
              Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
              startActivityForResult(signInIntent, RC_SIGN_IN);
              break;
      }
  }
  
  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
  
      // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
      if (requestCode == RC_SIGN_IN) {
          GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
          handleSignInResult(result);
      }
  }
  
  private void handleSignInResult(GoogleSignInResult result) {
      Log.d(TAG, "handleSignInResult:" + result.isSuccess());
      if (result.isSuccess()) {
          // Signed in successfully, show authenticated UI.
          GoogleSignInAccount account = result.getSignInAccount();
          firebaseAuthWithGoogle(account);
          Log.d(TAG, "google handleSignInResult: success!");
          Intent startMainActivity = new Intent(LoginActivity.this, MainActivity.class);
          startActivity(startMainActivity);
      } else {
          // Signed out, show unauthenticated
          Toast.makeText(LoginActivity.this, "HandleSignInResult malfunction. Try again.",
                  Toast.LENGTH_SHORT).show();
          Intent startMainActivity = new Intent(LoginActivity.this, LoginActivity.class);
          startActivity(startMainActivity);
      }
  }
  
  private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
      Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId());
  
      AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
      fbAuth.signInWithCredential(credential)
              .addOnCompleteListener(this, new OnCompleteListener&lt;AuthResult&gt;() {
                  @Override
                  public void onComplete(@NonNull Task&lt;AuthResult&gt; task) {
                      if (task.isSuccessful()) {
                          // Sign in success, update UI with the signed-in user's information
                          Log.d(TAG, "signInWithCredential:success");
                          FirebaseUser user = fbAuth.getCurrentUser();
                      } else {
                          // If sign in fails, display a message to the user.
                          Log.w(TAG, "signInWithCredential:failure", task.getException());
                          Toast.makeText(LoginActivity.this, "Authentication failed.",
                                  Toast.LENGTH_SHORT).show();
                          Intent startMainActivity = new Intent(LoginActivity.this, LoginActivity.class);
                          startActivity(startMainActivity);
                      }
  
                  }
              });
  }
  }
  </code></pre>