1

I am currently developing an android app that only requires the user to sign up once after pressing start. The next time the user opens the application, pressing start will redirect the user to the main game already.

I was able to do this using SharedPreferences. After signing up, I store a value called currentState and I use it to check if the user has already signed up. Here's the code:

   final int currentState = sharedPreferences.getInt(USERSTATE_NUM_KEY,0);
   if(currentState == 0 ) {
          Intent i = new Intent(MainActivity.this, Main_Screen.class);
          startActivity(i);
    } else{
               // Redirect user to tutorial page, then the map page.
    }

Now I was asked to accomplish this again using Firebase since I've already stored the user data.

Here's my code so far just to try if it's going to the onDataChange but it doesn't and I'm not sure how to do it now. Here's my code so far:

final String currentUsername = sharedPreferences.getString(USERNAME_NUM_KEY, "");
final DatabaseReference userNameRef = rootReference.child("users");
startGame.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            userNameRef.orderByChild("username").equalTo(currentUsername)
                    .addListenerForSingleValueEvent(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                            if(dataSnapshot.exists()){
                                Log.v("TESTERR", "HELLO I EXIST LOL");
                            }
                            else{
                                Log.v("TESTERR", "NEW USER PAGE");
                            }
                        }

                        @Override
                        public void onCancelled(DatabaseError databaseError) {

                        }
                    });
 }
 });

firebase databasee

Thank you!!!

5
  • We have an Android project that requires us to use Services. My group mate told me to do it again to further utilize the database because we're just using it now for our leaderboard page. Commented Apr 3, 2018 at 12:06
  • but why do you want to use ondatachange to check if user is logged in? Have you used firebase authentication? Commented Apr 3, 2018 at 12:20
  • actually i was just searching for how to actually check if the user already exists when the application starts. what i really need is just to check if its in the database but the only answer i could find was the one above Commented Apr 3, 2018 at 12:27
  • yes, what don't you like with the answer above? Commented Apr 3, 2018 at 12:28
  • with the new code, everytime i click start i dont think im getting the snapshot. im new to firebase and im not sure why the "datasnapshot.exists" is inside ondatachanged thats why i was trying to see if it was going to go through the method Commented Apr 3, 2018 at 12:33

1 Answer 1

5

This:

userNameRef.orderByChild("username").equalTo(currentUsername)

is a query it is like saying where username= currentUsername

The currentUsername is the logged in user, the datasnapshot is users.

So in your database you have:

users
 userid
   username: currentUsername

When you use if(dataSnapshot.exists()){ it checks the snapshot which is users and checks the where condition, if theys exists in the database then it will enter the if clause.

If the where condition does not exists in the database then it will enter the else.

Sign up to request clarification or add additional context in comments.

2 Comments

It's weird though because nothing shows up on my Log making me think that it doesn't really enter the method at all?
check if it is "users" and not "Users", also inside the equalTo write the name: "kanyewest" as in the database and test it

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.