Android App Development | Lecture#36 | Hive Learners

𝓖𝓻𝓮𝓮𝓽𝓲𝓷𝓰𝓼

Hello, dear Hive Learner, I hope you all are well. Yesterday we added the email verification to our sign-in activity. Today we will check if the user is login and is email verified on app start so that we can open the welcome screen. On the Welcome screen, we will show the login user email and username. Also, write the firebase signout code in the logout button and click listeners.

GitHub Link

Use this GitHub project to clone into your directory. The following lecture will constantly update it so you will never miss the latest code. Happy Coding!

What Should I Learn

  • Check If the user has already login
  • Check if the email is verified or not

Assignment

  • Open the Welcome screen if the user sign-in and with a verified email

Procedure

In MainActivity.java we will add the check in onStart() override. We also learn all the overrides in our previous lectures. Declare and initialize the FirebaseAuth in the MainActivity.

Now add the override onStart() by pressing CTRL+O.

Now we will add the login in the onStart. It will open the Welcome Activity if a user is already login and if the email is verified.

 @Override
    protected void onStart() {
        super.onStart();

        if (firebaseAuth.getCurrentUser() != null) {
            if (firebaseAuth.getCurrentUser().isEmailVerified()) {
                startActivity(new Intent(MainActivity.this, Welcome_Activity.class));
                finish();
            }
        }
    }

In the Welcome Activity, we will show the username. We will remove the SharedPreference code and add the FirebaseAuth code in Welcome Activity. First, we need to declare and initialize the FirebaseAuth then we can use it.

We used the custom dialog in the Welcome Activity logout button click listener. So we keep the code and only change the SharedPreference logout with the Firebase logout. We will also remove the ProgressDialog as there is no listener for firebase signout. It will sign out the user on the spot.

 logout_btn.setOnClickListener(v -> {

            Dialog custom_dialog = new Dialog(Welcome_Activity.this, R.style.Custom_Dialog);
            custom_dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            custom_dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            custom_dialog.setContentView(R.layout.custom_dialog);

            MaterialButton no_btn = custom_dialog.findViewById(R.id.dialog_no_btn);
            MaterialButton yes_btn = custom_dialog.findViewById(R.id.dialog_yes_btn);

            no_btn.setOnClickListener(v12 -> custom_dialog.cancel());

            yes_btn.setOnClickListener(v1 -> {
                firebaseAuth.signOut();
                startActivity(new Intent(Welcome_Activity.this, MainActivity.class));
                Toast.makeText(Welcome_Activity.this, "Logout Successful", Toast.LENGTH_SHORT).show();
                finish();
            });
            custom_dialog.show();

        });


hl_divider.png

Thank You

hl_footer_banner.png

H2
H3
H4
3 columns
2 columns
1 column
4 Comments