0

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();
    }
}
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
S.Byrne
  • 75
  • 1
  • 8
  • What is the problem in your code right now? I have explained step by step in one of my [tutorials](https://www.youtube.com/playlist?list=PLn2n4GESV0AmXOWOam729bC47v0d0Ohee) how to implement [Google](https://www.youtube.com/watch?v=bwgMWBhObDw&index=5&t=4s&list=PLn2n4GESV0AmXOWOam729bC47v0d0Ohee) and [Firebase](https://www.youtube.com/watch?v=UIRt9Ts0fRU&index=6&t=2s&list=PLn2n4GESV0AmXOWOam729bC47v0d0Ohee) authentication. – Alex Mamo Mar 13 '18 at 11:14
  • @AlexMamo I want that after the user enter their name and address successfully in the profile activity that it loads the MainMenu Activity and that the next time the user logs in it will check that they have there information stored in the database and go straight to the MainMenu Activity instead. – S.Byrne Mar 13 '18 at 14:24
  • Take also a look at my answer from **[this post](https://stackoverflow.com/questions/49253026/firebase-auth-and-database/49256810)**. – Alex Mamo Mar 13 '18 at 14:45
  • @AlexMamo Thanks but neither are what I'm looking to achieve here. – S.Byrne Mar 13 '18 at 15:01

0 Answers0