title: Android webview get current url and check if it contains a string
body: <p>The previous post is from here <a href="https://stackoverflow.com/questions/42392589/android-studio-webview-get-data-and-onfinish/42392837#42392837">android studio webview get data and onfinish</a> . I actually cannot get what I want from those answers. Maybe I should ask it more clearly. I actually used webview to load a content page. Then inside the content page, there are some links which are also local html created by myself. So the flow is like </p>
  
  <p>android activity -> webview -> content page -> game page. </p>
  
  <p>I actually want to retrieve a local variable <code>marks</code> in the game page. So I created a <code>exit</code> button, which is used to close the page and back to the android activity as well as carry the <code>marks</code> back to the android activity. </p>
  
  <p>So I searched some information myself, and found that <code>window.close()</code> doesn't work. So I change the current URL to indicate that the exit button is clicked. And then extract the <code>marks</code> from the URL and store it in the android activity. But it doesn't work with my codes below, I think it is probably because of the method is used, when I am in the content page. I hope someone could help to do this, as I really cannot find the problem. I have edited the codes as below:</p>
  
  <p>My activity in android:</p>
  
  <pre><code>private WebView webview;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  
      //setContentView(R.layout.activity_main);
  
      webview = new WebView(this);
      webview.getSettings().setJavaScriptEnabled(true);
  
      try {            
          webview.loadUrl("file:///android_asset/index.html");
          webview.setWebViewClient(new WebViewClient()
  
          @Override 
          public boolean shouldOverrideUrlLoading(WebView view, String url)
          {
             if (url.contains("?marks=")){
               finish();}  // close activity
             else{
               view.loadUrl(url);}
  
             return true; 
          });
      }
      catch(Exception ex)
      {
          ex.printStackTrace();
      }
      setContentView(webview);
  }
  </code></pre>
  
  <p>My exit button is a div:</p>
  
  <pre><code>&lt;div class="exit" onclick="finish()"&gt;Exit&lt;/div&gt;
  </code></pre>
  
  <p>I am going to use the <code>finish()</code> function to return the variable and close the webpage back to the activity in android.</p>
  
  <pre><code>function finish() {
    document.location.href= window.location.href +"?marks=" + marks;
  }
  </code></pre>