Categories

Follow Us @web_learners

Monday, August 10, 2020

How to Show Exit Dialog in Android App || Exit Dialog on Home Screen

 Hi, In the previous post we have learned How to Show Exit Dialogue on Splash Screen in Webview App. Today we will learn How to show Exit Dialog in Home Screen (Main Activity). Let's Start.

As we will show Exit dialog in MainActivity.java, so we will set a function onBackPressed().In that onBackPressed() function we will take an if-else condition.

Code:


The "if" condition will be if our website or app can go back then it will go to the previous page,  else it will show Exit dialog. And in the exit dialog, there will be a message, such as "Do you want to Exit?" or anything you want to show to your users.


And there will also two buttons in the Exit dialog, One is the "Yes" button, another one is the "No" Button. Then we have to set "OnClickListener" on "Yes" and "No" both button. In that OnClickListener, we will define what will happen after the user clicks on buttons.


After that, we will set if the user clicks on the "yes" button app will Exit successfully. And if the users click on the "No" button the user will stay on the current page. Then have to show the alert dialog.


So all the code together will be:

Code:
 

And see the below code so that you can understand where you have put the above Exit Dialog code.


Code:
 


Now you can run your project. You can see below the image we have successfully added Exit dialog on Home Screen. Exit dialog working perfectly.


That's it. Thank you.

No comments:

Post a Comment