Hazal A. Kara
Hazal A. Kara

Reputation: 1

Won't save data to Firebase Database in Android

I'm not sure what the problem is. I'm a beginner developer and I coded a registration/login page for an Android app I'm working on. New users are saved in Firebase Authorization but not in Firebase Database. My current rules are set to false but when I try to set them to true, the app keeps returning to the SetupActivity rather than the MainActivity. The app works fine when the rules are set to false but as I said, nothing appears in the Database. Here is my code:

public class SetupActivity extends AppCompatActivity {

private EditText FullName, EmailAddress, Password, CountryName;
private Button SaveInfoButton;
private ProgressDialog LoadingBar;
private CircleImageView ProfileImage;
private FirebaseAuth register_auth;
private DatabaseReference userreference;
private String currentUserID;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_setup);

    register_auth = FirebaseAuth.getInstance();
    currentUserID = register_auth.getCurrentUser().getUid();
    userreference = FirebaseDatabase.getInstance().getReference().child("Users").child(currentUserID);

    FullName = findViewById(R.id.name_setup);
    EmailAddress = findViewById(R.id.email_setup);
    Password = findViewById(R.id.password_setup);
    CountryName = findViewById(R.id.country_setup);
    SaveInfoButton = findViewById(R.id.save_button);
    ProfileImage = findViewById(R.id.profile_setup);
    LoadingBar = new ProgressDialog(this);


    SaveInfoButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view)
        {
            CreateNewAccount();
        }
    });

}

private void CreateNewAccount() {
    String full_name = FullName.getText().toString();
    String email = EmailAddress.getText().toString();
    String password = Password.getText().toString();
    String country = CountryName.getText().toString();

    if(TextUtils.isEmpty(email)) {
        Toast.makeText(this, "Please enter email.", Toast.LENGTH_SHORT).show();
    }
    else if(TextUtils.isEmpty(full_name)) {
        Toast.makeText(this, "Please enter your name.", Toast.LENGTH_SHORT).show();
    }
    else if(TextUtils.isEmpty(password)) {
        Toast.makeText(this, "Please enter password.", Toast.LENGTH_SHORT).show();
    }
    else if(TextUtils.isEmpty(country)) {
        Toast.makeText(this, "Please enter country.", Toast.LENGTH_SHORT).show();
    }
    else {

        LoadingBar.setTitle("Creating new account!");
        LoadingBar.setMessage("Please wait while your account is being created.");
        LoadingBar.show();
        LoadingBar.setCanceledOnTouchOutside(true);

        register_auth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if(task.isSuccessful()) {
                    LoadingBar.dismiss();
                    Toast.makeText(SetupActivity.this, "Registration was successful!", Toast.LENGTH_SHORT).show();
                    SaveAccountInformation();
                }
                else {
                    String message = task.getException().getMessage();
                    Toast.makeText(SetupActivity.this, "Registration unsuccessful." + message, Toast.LENGTH_SHORT).show();
                    LoadingBar.dismiss();
                }

            }
        });
    }
}

private void SaveAccountInformation() {

    String full_name = FullName.getText().toString();
    String country = CountryName.getText().toString();

    Map<String, Object> childUpdates = new HashMap<>();
    childUpdates.put("fullname", full_name);
    childUpdates.put("country", country);
    childUpdates.put("status", "Hey there, I am using Study Guide!");
    childUpdates.put("birthday", "none");

    userreference.updateChildren(childUpdates).addOnCompleteListener(new OnCompleteListener() {
        @Override
        public void onComplete(@NonNull Task task) {
            if (task.isSuccessful()) {
                SendToLogin();
            }
            else {
                String message = task.getException().getMessage();
                Toast.makeText(SetupActivity.this, "An error occurred. " + message, Toast.LENGTH_SHORT).show();
            }
        }
    });
}

private void SendToLogin() {
    Intent LoginIntent = new Intent(SetupActivity.this,LoginActivity.class);
    LoginIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    startActivity(LoginIntent);
    finish();
}

}

If someone could point me in the right direction or let me know what I'm doing wrong, it will be very much appreciated!

Upvotes: 0

Views: 356

Answers (2)

Anshuman Singh
Anshuman Singh

Reputation: 71

You need to manually save the users in your Firebase Database once a new user is registered.

You can look at the docs on how to write data.

Upvotes: 0

Stackoverflower
Stackoverflower

Reputation: 322

Hazal you are not saving the data , you are updating the data so change your code

from

userreference.updateChildren(childUpdates)

To

userreference.setValue(childUpdates)

Upvotes: 1

Related Questions