Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何防止返回按钮直到确认消息?_Java_Android_Webview - Fatal编程技术网

Java 如何防止返回按钮直到确认消息?

Java 如何防止返回按钮直到确认消息?,java,android,webview,Java,Android,Webview,在Android Studio中,我使用了WebView。因此,如果用户单击后退按钮,我希望在应用程序关闭之前显示确认消息 这是我当前使用的代码,但它不是每次都有效 public void onBackPressed() { new AlertDialog.Builder(this) .setMessage("Are you sure you want to exit?") .setCancelable(false)

在Android Studio中,我使用了WebView。因此,如果用户单击后退按钮,我希望在应用程序关闭之前显示确认消息

这是我当前使用的代码,但它不是每次都有效

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();
}

您必须在活动中调用确认消息方法
onBackPressed
method

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

上面的代码运行良好。 如果要将WebView导航回上一页,请使用“下一页”

@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();
  }
}


你能试试吗。希望它能工作

请将您的代码粘贴到调用
onBackPressed()
method的位置,或者您可以直接从onBackPressed打开对话框,正如问题中所述。谢谢,现在我使用了super.onBackPressed();而不是完成();它看起来像它现在的工作
 @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();
    }