0

I am trying to open a fragment upon successful login. I realized that intent cannot be used for fragments and thus used FragmentManager. However, when I try using FragmentManager, my app crashes. I have no errors showing up in the code. The following code shown is in my activity.

 package com.example.ireview;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.example.ireview.ui.home.HomeFragment;
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;
import com.google.firebase.auth.FirebaseUser;

public class LogInPage extends AppCompatActivity {

    private FirebaseAuth mAuth = FirebaseAuth.getInstance();
    private FirebaseUser currentUser = mAuth.getCurrentUser();
    EditText email;
    EditText password;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_log_in_page);

        email = (EditText) findViewById(R.id.editTextEmailLogIn);
        password = (EditText) findViewById(R.id.editTextPasswordLogIn);

    }


    public void clickLogin(View v) {
        String userEmail = email.getText().toString();
        String userPassword = password.getText().toString();

        if (userEmail.length() != 0 && userPassword.length() != 0) {
            mAuth.signInWithEmailAndPassword(userEmail, userPassword)
                    .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                        @Override
                        public void onComplete(@NonNull Task<AuthResult> task) {
                            if (task.isSuccessful()) {
                                // Sign up success
                                Toast.makeText(getBaseContext(), "Login Successful.",
                                        Toast.LENGTH_SHORT).show();
                                // Bring user to success activity

                                androidx.fragment.app.Fragment fragment = new Fragment();
                                FragmentManager fragmentManager = getSupportFragmentManager();
                                fragmentManager.beginTransaction().replace(R.id.home_frag, fragment).commit();



                            } else {
                                //
                            }
                        }
                    });
        } else {
            //
        }
    }
}

1 Answer 1

1

create object of your fragment like

HomeFragment fragment = new HomeFragment();

instead of

androidx.fragment.app.Fragment fragment = new Fragment();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.