Android App Development | Lecture#36 | Hive Learners

in Hive Learners โ€ข 2 months ago

๐“–๐“ป๐“ฎ๐“ฎ๐“ฝ๐“ฒ๐“ท๐“ฐ๐“ผ

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

Sort: ย 

Good check
!1UP

You can earn passive income by delegation of tribe tokens to "The Cartel".

dlmmqb-TheCartel-banner
Click this banner to join "The Cartel" discord server to know more.

1UP-PIZZA.png

You have received a 1UP from @gwajnberg!

The @oneup-cartel will soon upvote you with:
@stem-curator
And they will bring !PIZZA ๐Ÿ•.

Learn more about our delegation service to earn daily rewards. Join the Cartel on Discord.

Thanks for your contribution to the STEMsocial community. Feel free to join us on discord to get to know the rest of us!

Please consider delegating to the @stemsocial account (85% of the curation rewards are returned).

You may also include @stemsocial as a beneficiary of the rewards of this post to get a stronger support.ย 
ย 

Congratulations @faisalamin! You have completed the following achievement on the Hive blockchain and have been rewarded with new badge(s):

You received more than 25000 upvotes.
Your next target is to reach 30000 upvotes.

You can view your badges on your board and compare yourself to others in the Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

Check out the last post from @hivebuzz:

Hive Power Up Month Challenge 2022-07 - Winners List
The 8th edition of the Hive Power Up Month starts today!
Hive Power Up Day - August 1st 2022