-1

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.

Edric
  • 24,639
  • 13
  • 81
  • 91
Pranav Mishra
  • 15
  • 1
  • 5
  • Does this answer your question? [Android: Storing username and password?](https://stackoverflow.com/questions/1925486/android-storing-username-and-password) – Edric Dec 08 '19 at 07:26
  • Can you please provide some code how you are navigating to the next activity? – Swayangjit Dec 08 '19 at 07:28

2 Answers2

1

You have to call finish after starting new activity as below

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("");
finish();
Ganesh Kanna
  • 2,269
  • 1
  • 19
  • 29
  • can you also tell how to initialise welcome activity when app relaunches in case when user is logged in – Pranav Mishra Dec 08 '19 at 07:41
  • Make Welcome activity as launching activity and navigate to login activity if no user persisted. Otherwise navigate to main activity. – Ganesh Kanna Dec 08 '19 at 07:44
0

Use like following

 Intent int31 = new Intent(MainActivity.this,welcome.class);
 int31.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
 startActivity(int31);
Swayangjit
  • 1,875
  • 2
  • 13
  • 22