0

I am very new to android studio and could really use your help. i am having problem with logIn page code where after login using firebase auth. i would retrive a int value from firestore in login page and then decide where to go next(layout). it always goes to main activity rather personaldetails2(i.e, the if condition is always false even the value inside fristtimekey = 1).

please check where the problem is at if(fristtimekey ==1), other then that ever thing is correct(logcat is correct too with value 1 in it).

here my code:

fAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
    @Override
    public void onComplete(@NonNull Task<AuthResult> task) {
        if (task.isSuccessful()) {
            Toast.makeText(Login.this, "LogedIn succesfully", Toast.LENGTH_SHORT).show();

            userid = fAuth.getCurrentUser().getUid();
            DocumentReference docRef = foster.collection("users").document(userid);
            DocumentReference document = foster.document("users/email");
            docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                @Override
                public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                    if (task.isSuccessful()) {
                        DocumentSnapshot document = task.getResult();
                        if (document.exists()) {
                            fristtimekey = document.getLong("Frist time");
                            Log.d("TAG", String.valueOf(fristtimekey));
                            Log.d("TAG", "DocumentSnapshot data: " + document.getLong("Frist time") + " int: " + fristtimekey);
                        } else {
                            Log.d("TAG", "No such document");
                        }
                    } else {
                        Log.d("TAG", "get failed with ", task.getException());
                    }
                }
            });
            progressBar.setVisibility(View.GONE);
            if (fristtimekey == 1) {
                startActivity(new Intent(getApplicationContext(), MainActivity.class));
            } else {
                startActivity(new Intent(getApplicationContext(), PersonalDetails2.class));
            }
        } else {
            Toast.makeText(Login.this, "LogedIn unsuccesfully", Toast.LENGTH_SHORT).show();
            progressBar.setVisibility(View.GONE);
        }
    }
}); 
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807

1 Answers1

0

Data is loaded from Firestore (and most modern cloud APIs) asynchronously, and your main code continues while that is happening. Then when the data is available, your onComplete is called with it, so that you can use that data.

In your current code this means that your if (fristtimekey == 1) { [sic] runs before the fristtimekey = document.getLong("Frist time") ever runs. If you set breakpoints on these lines and run the code in a debugger, or add some logging around the lines you can most easily see this.

What this means in practice is that any code that needs the data from the database, must be inside onComplete, be called from there, or be otherwise synchronized.

Applying the above to your code means:

docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
        progressBar.setVisibility(View.GONE); // 

        if (task.isSuccessful()) {
            DocumentSnapshot document = task.getResult();
            if (document.exists()) {
                fristtimekey = document.getLong("Frist time");
                Log.d("TAG", String.valueOf(fristtimekey));
                Log.d("TAG", "DocumentSnapshot data: " + document.getLong("Frist time") + " int: " + fristtimekey);
            } else {
                Log.d("TAG", "No such document");
            }
            // 
            if (fristtimekey == 1) {
                startActivity(new Intent(getApplicationContext(), MainActivity.class));
            } else {
                startActivity(new Intent(getApplicationContext(), PersonalDetails2.class));
            }
        } else {
            Log.d("TAG", "get failed with ", task.getException());
        }
    }
});

I recommend also checking out:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807