title: How to write code in java when webview app reaches specific page it should exit the app with android back button?
body: <p>I'm trying to write code in java when the webview reaches a specific page for example index it should exit the app but I failed till now.</p>
  
  <p>here is my code:-</p>
  
  <pre><code> @Override
  public void onBackPressed() {
      if (mWebView.canGoBack()) {
          mWebView.goBack();
      } else {
          String url = "file:///android_asset/www/index.html" ;
          if (url.startsWith("file:///android_asset/www/index.html")) {
              new AlertDialog.Builder(this)
                      .setIcon(android.R.drawable.ic_dialog_alert)
                      .setTitle("Exit!")
                      .setMessage("Are you sure you want to close?")
                      .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                          @Override
                          public void onClick(DialogInterface dialog, int which) {
                              finish();
                          }
  
                      })
                      .setNegativeButton("No", null)
                      .show();
  
          }
  
  
      }
  }
  </code></pre>
  
  <p>Update:- Problem has been solved</p>
  
  <p>here is what I did:-</p>
  
  <pre><code>    @Override
  public void onBackPressed() {
          String url = mWebView.getUrl();;
          if (url.equals("file:///android_asset/www/index.html")) {
              new AlertDialog.Builder(this)
                      .setIcon(android.R.drawable.ic_dialog_alert)
                      .setTitle("Exit!")
                      .setMessage("Are you sure you want to close?")
                      .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                          @Override
                          public void onClick(DialogInterface dialog, int which) {
                              finish();
                          }
  
                      })
                      .setNegativeButton("No", null)
                      .show();
  
          }
      else {
              mWebView.goBack();
      }
  }
  </code></pre>
  
  <p>hope it will help someone in future</p>