I am working on a project in which there is a login screen asking users to enter email and password. If login credentials are correct the app will open welcome activity. The problem is that when the back button is clicked the app takes them back to the login screen asking to log in again. I want it to exit the app when the back button is tapped. Is there a method to do so?
Also, I want that while the user is logged in, the app starts directly with the welcome screen and not with the login screen. I am using Firebase to store the user's info.
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String email= editText.getText().toString();
String pwd = editText2.getText().toString();
if(email.isEmpty()){
editText.setError("Please enter your email id");
editText.requestFocus();
}
else if(pwd.isEmpty()){
editText2.setError("Please enter your password");
editText2.requestFocus();
}
else{
progressBar.setVisibility(View.VISIBLE);
mfirebaseAuth.signInWithEmailAndPassword(editText.getText().toString(), editText2.getText().toString()).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful() && mfirebaseAuth.getCurrentUser().isEmailVerified()) {
Intent int31 = new Intent(MainActivity.this,welcome.class);
int31.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
int31.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(int31);
progressBar.setVisibility(View.INVISIBLE);
editText.setText("");
editText2.setText("");
} else if (task.isSuccessful() && !(mfirebaseAuth.getCurrentUser().isEmailVerified())){
Toast.makeText(MainActivity.this,"Please verify your email address and try again!",Toast.LENGTH_SHORT).show();
editText.setText("");
editText2.setText("");
progressBar.setVisibility(View.INVISIBLE);
}
else{
Toast.makeText(MainActivity.this,task.getException().getMessage(),Toast.LENGTH_SHORT).show();
editText.setText("");
editText2.setText("");
progressBar.setVisibility(View.INVISIBLE);
}
}
});
The code above is of the initial screen which is opening every time, irrespective whether the user is logged in or not.