Using Firebase open a different activity if user logged in second time



I am using Firebase in my project. What I want?? When a new user logged in, he goes through all activities (like Information activity, etc) after login and then to the dashboard. when that user logout from the app and logged in again then directly go to dashboard not need to fill that all information again.

 mAuth.signInWithEmailAndPassword(emailLogin, passwordLogin).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {

                    if (task.isSuccessful()) {
                        if (mAuth.getCurrentUser() != null) {
                            startActivity(new Intent(LoginActivity.this, Bottom_Navigation_Bar.class));
                        } else {


                            Intent intent = new Intent(LoginActivity.this, InformationActivity.class);
                            startActivity(intent);
                            Toast.makeText(LoginActivity.this, "Sign In , Successful...", Toast.LENGTH_SHORT).show();
                        }
                    } else {
                        Toast.makeText(LoginActivity.this, "Error Occurred, while Signing In... ", Toast.LENGTH_SHORT).show();
                    }

                }
            });

I am using this code, but it takes both new and old users to the dashboard.

Answer

Simply checking:

if (mAuth.getCurrentUser() != null) { ... }

Doesn’t help you know if the user signs in for the first time, it only checks if the user is authenticated or not. To know is the user signs in for the first time, please use the following lines of code:

mAuth.signInWithCredential(credential).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
    @Override
    public void onComplete(@NonNull Task<AuthResult> task) {
        boolean isNewUser = task.getResult().getAdditionalUserInfo().isNewUser();
        if (isNewUser) {
            Log.d(TAG, "The user is new!"); //Send the user through all activities
        } else {
            Log.d(TAG, "The user is NOT new!"); //Send the user to the dashboard
        }
    }
});

Edit:

According to your comment:

What does getAdditionalUserInfo() method do?

task.getResult() returns an object of type AuthResult. That being said, we can call the above method on such an object, to return an object of type AdditionalUserInfo. As you can see in the docs, there is a method there called isNewUser() that:

Returns whether the user is new or existing



Source: stackoverflow