I have recently adopted Firebase for my Backend work. Using Firebase I want to store all the user data like username, phone number, etc to Firebase in the same RegisterActivity
and not just Email & Password. How can I achieve this ?
My RegisterActivity
will only appear at the time of installation. When user have register to my app, I am destroying the activity. So, there is no instance of RegisterActivity
further.
RegisterActivity - onCreate()
:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Firebase.setAndroidContext(this);
setContentView(R.layout.activity_register);
initialization();
underlineText(); //Underlining Text in App
userObj = new User();
userObj.setName(NAME);
userObj.setEMAIL(EMAIL);
userObj.setPHONE(PHN);
animShake = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.shake); //Animation
vib = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); //Vibration
reg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
submitForm(); //Registration Click Listener
}
});
skip.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
skipRegistrationSection(); //Skip Click Listener
}
});
runAtInstallation();
}
submitform()
:
private void submitForm() {
if (!checkName()) {
name.setAnimation(animShake);
name.startAnimation(animShake);
vib.vibrate(60);
return;
}
if (!checkEmail()) {
email.setAnimation(animShake);
email.startAnimation(animShake);
vib.vibrate(60);
return;
}
if (!checkPhone()) {
phone.setAnimation(animShake);
phone.startAnimation(animShake);
vib.vibrate(60);
return;
}
if (!checkPassword()) {
password.setAnimation(animShake);
password.startAnimation(animShake);
vib.vibrate(60);
return;
}
if (!checkConfirmPassword()) {
confirmPassword.setAnimation(animShake);
confirmPassword.startAnimation(animShake);
vib.vibrate(60);
return;
}
nameLayout.setErrorEnabled(false);
emailLayout.setErrorEnabled(false);
phoneLayout.setErrorEnabled(false);
passwordLayout.setErrorEnabled(false);
confirmPasswordLayout.setErrorEnabled(false);
NAME = name.getText().toString().trim();
EMAIL = email.getText().toString().trim();
PHN = phone.getText().toString().trim();
PASSWORD = password.getText().toString();
progressBar.setVisibility(View.VISIBLE);
authUser(); //authenticating User via Email & Password
}
authUser()
:
private void authUser() {
mFirebaseAuth.createUserWithEmailAndPassword(EMAIL, PASSWORD).addOnCompleteListener(RegisterActivity.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (!task.isSuccessful()) {
AlertDialog.Builder builder = new AlertDialog.Builder(RegisterActivity.this);
builder.setMessage(task.getException().getMessage())
.setTitle("Error")
.setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.show();
progressBar.setVisibility(View.GONE);
} else {
progressBar.setVisibility(View.GONE);
Intent intent = new Intent(RegisterActivity.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
}
});
}
runAtInstallation
:
private void runAtInstallation() {
SharedPreferences pref = getSharedPreferences("ActivityPREF", Context.MODE_PRIVATE);
if (pref.getBoolean("activity_executed", false)) {
Intent act = new Intent(getApplicationContext(), MainActivity.class);
act.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
act.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(act);
finish();
} else {
SharedPreferences.Editor ed = pref.edit();
ed.putBoolean("activity_executed", true);
ed.commit();
}
}
I want to save name,email,& phone in the firebase database during registration and to destroy the activity after that.