title: Starting a fragment from the onCompleteListener used for FirebaseAuth?
body: <p>I'm trying to open a fragment from another fragment but am getting errors. </p>
  
  <p>The app opens on a home screen with a bottomNav view. Clicking one of the tabs opens a fragment with a login screen for Firebase users. After entering details and pressing login button, it's supposed to verify user and open another fragment. </p>
  
  <p>Everything works fine until you press login. </p>
  
  <p>Below is a working example where both fragments are actually activities and everything works fine.</p>
  
  <pre><code>import android.content.Intent;
  import android.os.Bundle;
  import android.support.annotation.NonNull;
  import android.support.v7.app.AppCompatActivity;
  import android.text.TextUtils;
  import android.view.View;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.Toast;
  
  import com.google.android.gms.tasks.OnCompleteListener;
  import com.google.android.gms.tasks.Task;
  import com.google.firebase.auth.AuthResult;
  import com.google.firebase.auth.FirebaseAuth;
  
  public class LoginActivity extends AppCompatActivity implements View.OnClickListener{
  
  public Button btnLogIn;
  private EditText inputEmail, inputPassword;
  private FirebaseAuth mAuth;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      mAuth = FirebaseAuth.getInstance();
  
      if (mAuth.getCurrentUser() != null) {
          startActivity(new Intent(LoginActivity.this, SecondPage.class));
          finish();
      }
  
      setContentView(R.layout.activity_login);
  
      btnLogIn = findViewById(R.id.btn_login);
      inputEmail = findViewById(R.id.email);
      inputPassword = findViewById(R.id.password);
  
      findViewById(R.id.btn_login).setOnClickListener(this);
  
      mAuth = FirebaseAuth.getInstance();
  }
  
  @Override
  public void onClick(View view){
      final String email = inputEmail.getText().toString();
      final String password = inputPassword.getText().toString();
  
      if (TextUtils.isEmpty(email)) {
          Toast.makeText(getApplicationContext(), "Enter email address!", Toast.LENGTH_SHORT).show();
          return;
      }
  
      if (TextUtils.isEmpty(password)) {
          Toast.makeText(getApplicationContext(), "Enter password!", Toast.LENGTH_SHORT).show();
          return;
      }
  
      mAuth.signInWithEmailAndPassword(email, password)
              .addOnCompleteListener(this, new OnCompleteListener&lt;AuthResult&gt;(){
                  @Override
                  public void onComplete(@NonNull Task&lt;AuthResult&gt; task) {
                      if (!task.isSuccessful()) {
                          if (password.length() &lt; 6) {
                              inputPassword.setError(getString(R.string.minimum_password));
                          } else {
                              Toast.makeText(LoginActivity.this, getString(R.string.auth_failed), Toast.LENGTH_LONG).show();
                          }
                      } else {
                          Intent intent = new Intent(LoginActivity.this, SecondPage.class);
                          startActivity(intent);
                          finish();
                      }
                  }
              });
  }
  
  }
  </code></pre>
  
  <p>Now here is that same code, except now the LoginActivity is a fragment and needs to open another fragment. This is what gives me errors. I have changed what is needed for the fragment itself.</p>
  
  <pre><code>package com.nikelspot.app;
  
  import android.content.Intent;
  import android.os.Bundle;
  import android.support.annotation.NonNull;
  import android.app.Fragment;
  import android.text.TextUtils;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.Toast;
  
  import com.google.android.gms.tasks.OnCompleteListener;
  import com.google.android.gms.tasks.Task;
  import com.google.firebase.auth.AuthResult;
  import com.google.firebase.auth.FirebaseAuth;
  
  public class LoginActivity extends Fragment implements View.OnClickListener {
  
      public LoginActivity() {
          //needed empty constructor
      }
  
      public Button btnLogIn;
      private EditText inputEmail, inputPassword;
      private FirebaseAuth mAuth;
  
      @Override
      public View onCreateView(LayoutInflater inflater,
                               ViewGroup container,
                               Bundle savedInstanceState) {
  
          mAuth = FirebaseAuth.getInstance();
  
          if (mAuth.getCurrentUser() != null) {
              startActivity(new Intent(getActivity(), SecondPage.class));
          }
  
          View rootView = inflater.inflate(R.layout.frag_login,
                  container, false);
  
          btnLogIn = rootView.findViewById(R.id.btn_login);
          inputEmail = rootView.findViewById(R.id.email);
          inputPassword = rootView.findViewById(R.id.password);
          mAuth = FirebaseAuth.getInstance();
  
          rootView.findViewById(R.id.btn_login).setOnClickListener(this);
          mAuth = FirebaseAuth.getInstance();
          return rootView;
      }
  
      @Override
      public void onClick(View view){
          final String email = inputEmail.getText().toString();
          final String password = inputPassword.getText().toString();
  
          if (TextUtils.isEmpty(email)) {
              Toast.makeText(getActivity(), "Enter email address!", Toast.LENGTH_SHORT).show();
              return;
          }
  
          if (TextUtils.isEmpty(password)) {
              Toast.makeText(getActivity(), "Enter password!", Toast.LENGTH_SHORT).show();
              return;
          }
  
          mAuth.signInWithEmailAndPassword(email, password)
                  .addOnCompleteListener(this, new OnCompleteListener&lt;AuthResult&gt;(){
                      @Override
                      public void onComplete(@NonNull Task&lt;AuthResult&gt; task) {
                          // If sign in fails, display a message to the user. If sign in succeeds
                          // the auth state listener will be notified and logic to handle the
                          // signed in user can be handled in the listener.
  
                          if (!task.isSuccessful()) {
                              // there was an error
                              if (password.length() &lt; 6) {
                                  inputPassword.setError(getString(R.string.minimum_password));
                              }
                              else {
                                  Toast.makeText(getActivity(), getString(R.string.auth_failed), Toast.LENGTH_LONG).show();
                              }
                          }
  
                          else {
                              Intent intent = new Intent(getActivity(), SecondPage.class);
                              startActivity(intent);
                              finish();
                          }
                      }
          });
      }
  
  }
  </code></pre>
  
  <p>In Android Studio, the only thing wrong is the finish() method below is highlighted red. The error message says it cannot be resolved. </p>
  
  <pre><code>  else {
            Intent intent = new Intent(getActivity(), SecondPage.class);
            startActivity(intent);
            finish();
          }
  </code></pre>
  
  <p>I know the finish() method cannot be called from within a fragment, but I have tried other solutions online and none of them have worked. This is the closest I can get.</p>
  
  <p>Thanks!</p>