Dwiin
Dwiin

Reputation: 21

How to prevent back button until confirmation message?

In Android Studio, I've used a WebView. So if a user clicks the back button, I want to show a confirmation message before app close.

This is my current code which I used, but it is not working every time

public void onBackPressed() {
    new AlertDialog.Builder(this)
            .setMessage("Are you sure you want to exit?")
            .setCancelable(false)
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    finish();
                }
            })
            .setNegativeButton("No", null)
            .show();
}

Upvotes: 2

Views: 1785

Answers (5)

@Override
public void onBackPressed() {
    if (webView.canGoBack()) {
        webView.goBack();
    }else {
    new AlertDialog.Builder(this)
            .setTitle("Are you sure?")
            .setMessage("Do you want to exit?")
            .setPositiveButton("Close", new DialogInterface.OnClickListener(){
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    finish();
                }
            })
            .setNegativeButton("No", null)
            .show();
}

This worked for me. Hope it works.

Upvotes: 0

Nilesh Panchal
Nilesh Panchal

Reputation: 1069

You have to call your confirmation message method inside your Activities onBackPressed Method

    @Override 
    public void onBackPressed() {
      if (mWebView.canGoBack()) {
          mWebView.goBack();
      } else {
        onBackPressed() // This is Your Confirmation Dialog method
      }
    }

Upvotes: 0

Rakesh R
Rakesh R

Reputation: 754

 @Override
    public void onBackPressed() {
        if (webView.canGoBack()) {
            // If web view have back history, then go to the web view back history
            webView.goBack();
        } else {
            // Ask the user to exit the app or stay in here
            exitApp();
        }
    }


public void exitApp() {
        final AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setTitle("Please confirm");
        builder.setMessage("Do you want to exit the app?");
        builder.setCancelable(true);

        builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
               finish();
            }
        });

        builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
            }
        });

        // Create the alert dialog using alert dialog builder
        AlertDialog dialog = builder.create();

        // Finally, display the dialog when user press back button
        dialog.show();
    }

Upvotes: 0

Tuan Nguyen
Tuan Nguyen

Reputation: 2607

enter image description here

Can you try with it. Hope it works

Upvotes: 0

deepak raj
deepak raj

Reputation: 3801

The above code is working fine. If you want to navigate the WebView back to the previous page use below one.

@Override
public void onBackPressed() {
  if (mWebView.canGoBack()) {
          mWebView.goBack();
  }else{
        new AlertDialog.Builder(this)
          .setMessage("Are you sure you want to exit?")
          .setCancelable(false)
          .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int id) {
                   super.onBackPressed();
              }
          })
          .setNegativeButton("No", null)
          .show();
  }
}

Upvotes: 5

Related Questions