title: How to show pop-up message when exit android webview app
body: <p>I just made my first Android webview application.</p>
  
  <p>It works fine but there's still one thing I'd like to add to the webview: an exit button.</p>
  
  <p><strong>How I like to do that:</strong></p>
  
  <p>When users click on the back button of the phone, they go back to the previous page that they visited. Now when they are back on the first page they visited and go back, the app automatically closes. There I would like to show a pop-up message that says "Are you sure you want to exit?"</p>
  
  <p>How can I display a message like that?</p>
  
  <p><strong>My code:</strong></p>
  
  <pre><code>// Open previous opened link from history on webview when back button pressed
  
  @Override
  // Detect when the back button is pressed
  public void onBackPressed() {
      if(webView.canGoBack()) {
          webView.goBack();
      } else {
          // Let the system handle the back button
          super.onBackPressed();
      }
  }
  </code></pre>
  
  <p><strong>EDIT:</strong> I made some changes to my code. This is the result::</p>
  
  <pre><code>private Boolean exit = false;
  @Override
  // Detect when the back button is pressed
  public void onBackPressed() {
      if(webView.canGoBack()) {
          webView.goBack();
      }
      else {
          if (exit)
              this.finish();
          else {
              Toast.makeText(this, "Press again to close.",
                  Toast.LENGTH_SHORT).show();
                  exit = true;
                  new Handler().postDelayed(new Runnable() {
                      @Override
                      public void run() {
                          exit = false;
                      }
                  }, 3 * 1000);
          }    
  
      // Let the system handle the back button
      super.onBackPressed();
      }
  }
  </code></pre>