Working on some basic database stuff with firebase. I have the information saving correctly and login and registration working but what I'm trying to achieve now is that when the user registers there information successfully in the profile activity it will open the MainMenu Activity and that the next time they login the app will know that they have already saved their user information in the database and open the MainMenu Activity and not the profile Activity. Thanks guys. Here's the code I have.
This is the MainActivity where the user logs in
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private Button buttonRegister;
private EditText editTextEmail;
private EditText editTextPassword;
private TextView textViewSignin;
private ProgressDialog progressDialog;
private FirebaseAuth firebaseAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
firebaseAuth = FirebaseAuth.getInstance();
progressDialog = new ProgressDialog(this);
if(firebaseAuth.getCurrentUser() != null){
//Profile activity
finish();
startActivity(new Intent(getApplicationContext(), ProfileActivity.class));
}
buttonRegister = (Button) findViewById(R.id.buttonRegister);
editTextEmail = (EditText) findViewById(R.id.editTextEmail);
editTextPassword = (EditText) findViewById(R.id.editTextPassword);
textViewSignin = (TextView) findViewById((R.id.textViewSignin));
buttonRegister.setOnClickListener(this);
textViewSignin.setOnClickListener(this);
}
private void registerUser(){
String email = editTextEmail.getText().toString().trim();
String password = editTextPassword.getText().toString().trim();
if(TextUtils.isEmpty(email)){
//email is empty
Toast.makeText(this, "Please enter email", Toast.LENGTH_LONG).show();
//stopping the function execution further
return;
}
if(TextUtils.isEmpty(password)){
//email is empty
Toast.makeText(this, "Please enter password", Toast.LENGTH_LONG).show();
//stopping the function execution further
return;
}
//if validations are Okay
//Register User
progressDialog.setMessage("Registering User...");
progressDialog.show();
firebaseAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
//user is successfully registered
// Toast.makeText(MainActivity.this, "Registered Successfully", Toast.LENGTH_LONG).show();
finish();
startActivity((new Intent(getApplicationContext(), ProfileActivity.class)));
}
else{
Toast.makeText(MainActivity.this, "Could not register, Please try again!", Toast.LENGTH_LONG).show();
}
progressDialog.dismiss();
}
});
}
@Override
public void onClick(View view) {
if(view == buttonRegister) {
registerUser();
}
if (view == textViewSignin){
//open login activity here
startActivity(new Intent(this,LoginActivity.class));
}
}
}
this is the profile activity
public class ProfileActivity extends AppCompatActivity implements View.OnClickListener {
private FirebaseAuth firebaseAuth;
private DatabaseReference databaseReference;
private TextView textViewUserEmail;
private Button buttonLogout, saveUserInformationBtn;
EditText userName, userAddress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
firebaseAuth = FirebaseAuth.getInstance();
if (firebaseAuth.getCurrentUser() == null) {
finish();
startActivity(new Intent(this, LoginActivity.class));
}
databaseReference = FirebaseDatabase.getInstance().getReference();
FirebaseUser user = firebaseAuth.getCurrentUser();
textViewUserEmail = (TextView) findViewById(R.id.textViewUserEmail);
saveUserInformationBtn = (Button) findViewById(R.id.userInfoSave);
userName = (EditText) findViewById(R.id.userName);
userAddress = (EditText) findViewById(R.id.userAddress);
textViewUserEmail.setText("Welcome " + user.getEmail());
buttonLogout = (Button) findViewById(R.id.buttonLogout);
buttonLogout.setOnClickListener(this);
saveUserInformationBtn.setOnClickListener(this);
}
private void saveUserInformation(){
String name = userName.getText().toString().trim();
String address = userAddress.getText().toString().trim();
Userinformation userinformation = new Userinformation(name, address);
FirebaseUser user = firebaseAuth.getCurrentUser();
databaseReference.child(user.getUid()).setValue(userinformation);
Toast.makeText(this, "Information Saved...", Toast.LENGTH_LONG). show();
}
@Override
public void onClick(View view) {
if(view == buttonLogout){
firebaseAuth.signOut();
finish();
startActivity(new Intent(this,LoginActivity.class));
}
if (view == saveUserInformationBtn){
saveUserInformation();
}
}
}