bisemanu
bisemanu

Reputation: 441

how to manage the back button on smartphone with alertdialog in my app

I would properly handle the pressure of the back button on your smartphone, I know That when is pressed displays an alertdialog That Asks, "Are you sure you want to quit by the APP" and if the users press again the app closes

Upvotes: 0

Views: 612

Answers (1)

waqaslam
waqaslam

Reputation: 68177

Overide the following method in the first Activity or the Activity where there is no return (except exit) of your application and show popup dialog to confirm the exit

@Override
public void onBackPressed() {
    DialogInterface.OnClickListener listener = new OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            YourActivity.this.finish();
        }
    };
    AlertDialog.Builder bldr = new Builder(YourActivity.this);
    bldr.setMessage("Are you sure you want to exit?");
    bldr.setPositiveButton(android.R.string.yes, listener);
    bldr.setNegativeButton(android.R.string.no, null);
    bldr.show();
}

Upvotes: 2

Related Questions