How to create a configuration screen in an Android app that appears only at the time of user registration?

Asked

Viewed 275 times

0

I’m developing an app with Firebase authentication, I need to put a screen in which the user will define a "nickname", but it must be immutable. So, this screen needs to appear right after registration and only this time, when accessing the same account by another device should not be possible to change the nickname.

I found some things about SharedPreferences, but I didn’t understand if the data saved like this stays only on the device, and how to do this with Firebase.

2 answers

4


How are you utilizing the firebase, I believe that it is not necessary to use the Sharedpreferences in this case, because as you want to define a nickname immutable if it does not exist, you will only need to apply the logic directly using the firebase. Sharedpreferences is useful in application configuration saving cases (such as themes, colors, splash screens, etc). You can read a little more about Sharedprefences here: Perform an action only when the app starts for the first time

Basically, you will need to check if the user has a nickname, if he has it, you do not show the nickname insertion screen, otherwise you show.

Your Ode will probably look like this:

{
    "Users": [
     {
        "userUUID/Key": {
            "name": "Corey",
            "apelido": ""
        }
     }
  ]
}

And the applied logic will be:

DatabaseReference db = FirebaseDatabase.getInstance().getReference("Users");

db.child(db.push().getKey() /* UUID do usuario */).addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        if(dataSnapshot.child("apelido").exists()) {
            // o apelido já foi definido
        } else {
           // o apelido ainda não foi definido
        }
    }

    @Override
    public void onCancelled(DatabaseError error) {
        Log.e(TAG, error.toException());
    }
});

You will also be able to check if the nickname exists in other instances. What I mean is, if the user finishes the registration but does not define a nickname, you can check if the nickname was set as soon as the application is opened, with this same logic, causing the user to come back to define it. It could be when the app opens or when the user logs into your account.

  • you could explain me better how to create this users Node?

  • https://firebase.google.com/docs/database/admin/save-data

  • We link codes above it saved on Node users using the user’s name. The code of my answer tells you to save through the UID, which is that code for each user of your app, understand? And so you will save all user data on that UID, which is unique.

2

Hello. Sharedpreferences is not a good option as the data is stored in the device itself. If the user logs into another device, this data will not be available. The ideal is to save this information in Firebase itself, preferably in the Database. Create a Node user type and use the user UID as key: user/6rh7zl2aZXhAkUDlQdIjVLCx61r2/... Inside this Node vc records your user object: name, email, nickname, etc. Take a read on write and recover data in Firebase Database: https://firebase.google.com/docs/database/android/read-and-write?hl=pt-br By logging into your app successfully, you get a UID by firebaseAuth. With this UID you recover the recorded object and go to the main screen of your app. If firebaseAuth is null, you redirect it to the registration screen. I hope I’ve helped.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.