In my Firebase app I store the IDs of the users. Then I want to store specific data under the users ID which works as it should. The problem I have is that when the app is reopened, the user specific data is deleted.
I suppose it is becuase the user ID is added or overwritten again so it deletes the children nodes.
If my spresumption is right, how can I make sure that my user ID is not overwritten? Is there a signUp method that only runs the first time the user gets his ID? I thought to store it in the signInAnonymously()
method, but it seems to be called on every start as well.
Here's my sign in code:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAuth = FirebaseAuth.getInstance();
mDatabase = FirebaseDatabase.getInstance().getReference();
// active listen to user logged in or not.
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
user = firebaseAuth.getCurrentUser();
if (user != null) {
// User is signed in
Log.d("TAG", "onAuthStateChanged:signed_in:" + user.getUid());
userID = user.getUid();
mDatabase.child("users").child(userID).setValue(userID);
Log.d("uid", userID);
} else {
// User is signed out
Log.d("TAG", "onAuthStateChanged:signed_out");
}
}
};
mAuth.signInAnonymously()
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Log.d("TAG", "OnComplete : " +task.isSuccessful());
if (!task.isSuccessful()) {
Log.d("TAG", "Authentication Failed : ", task.getException());
}
}
});
}