0

I'm working on a project in which I have to save details of the user before logging him/her in her account so that I don't have to fetch his/her detail again and can refer to that class whenever I need their details. It's displaying the toast "Error occurred ! please login again." which means null value or false is being returned. Also after I close the app and open again its moving to next Activity.

Screenshot of database

signintouserbycredential method

 private void signInTheUserByCredential(PhoneAuthCredential credential)
    {
        firebaseAuth=FirebaseAuth.getInstance();

        firebaseAuth.signInWithCredential(credential).addOnCompleteListener(Login_student.this, new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if(task.isSuccessful())
                {
                    String curr_user_str=task.getResult().getUser().getUid();
                    //Store user data in HelperClass for future use
                    if((curr_user_str.isEmpty())&&(storeUserInfoInHelperClass(curr_user_str)))
                    {
                        Toast.makeText(Login_student.this, "Login Successful", Toast.LENGTH_SHORT).show();
                        startActivity(new Intent(Login_student.this, Home_page.class));
                        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
                            finishAffinity();
                    }
                    else
                    {
                        Toast.makeText(Login_student.this,"Error occurred ! Please login again.",Toast.LENGTH_SHORT).show();
                    }

                }
                else
                {
                    Toast.makeText(Login_student.this, task.getException().getMessage(), Toast.LENGTH_SHORT).show();
                }

            }
        });
    }

storeInfoinHelperClass method

int flag=0;// to check if data is stored and if it is stored return true
    static HelperClass data;
    public boolean storeUserInfoInHelperClass(String curr_user_str) {
            reff = FirebaseDatabase.getInstance().getReference("userdetails");
            reff.child(curr_user_str).addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    if (data != null) {
                        data = dataSnapshot.getValue(HelperClass.class);
                        flag=1;
                    } else {
                        flag=0;
                    }

                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {

                }
            });
            if(flag==1)
                return true;
            else
                return false;

    }
  • `addValueEventListener` is asynchronous method, `storeUserInfoInHelperClass` might return even before `onDataChange` is called. – Hussain May 05 '20 at 06:52
  • ok, I see. So, what should be the approach? – LetsBhonest May 05 '20 at 07:00
  • What exactly in this code doesn't work the way you expect? What's wrong with it? – Alex Mamo May 05 '20 at 09:26
  • @AlexMamo I want to store the data of user in variable data of type HelperClass. What's happening is method is getting called but it's returning false so it's displaying the toast which I have made in the else part. I want to know what's wrong in my code that its not working as expected. – LetsBhonest May 05 '20 at 10:09
  • You cannot simply use the value of that `flag` outside the callback. Check **[this](https://stackoverflow.com/questions/47847694/how-to-return-datasnapshot-value-as-a-result-of-a-method/47853774)** out. – Alex Mamo May 05 '20 at 11:28

0 Answers0