title: WebViewClient return data to Activity onPageFinished
body: <p>I need to collect browser cookies from a WebView and return them to another Activity. Collecting the cookies works already but happens within a class that extends <code>WebViewClient</code>, and I don't know how to return the data from there.</p>
  
  <p>There are 2 Activities; MainActivity and LoginActivity, respectively. MainActivity has a single button that creates an Intent and starts LoginActivity, which has a single WebView. <code>OnCreate()</code>, the WebView is directed to the login page of a website. When the user successfully logs into this site, it stores data in the browser cookies - data that I need for an API call later. How can I return this data to MainActivity?</p>
  
  <p>The WebView has a custom WebViewClient with an overridden <code>OnPageFinished()</code> method, which if the URL is the one expected, will collect the browser cookies, as shown below.</p>
  
  <pre><code>class TempWebViewClient extends WebViewClient {
      @Override
      public void onPageFinished(WebView view, String url) {
          // If page URL is the Home page (ie., you logged in successfully), collect cookies.
          if (url.equals("https://slate.sheridancollege.ca/d2l/m/home")) {
              String cookies = CookieManager.getInstance().getCookie(url);
              String[] cookies2 = cookies.split(";");
              String key1 = "";
              String key2 = "";
  
              // Find Keys placed in browser cookies.
              for (String c : cookies2) {
                  String[] c2 = c.split("=");
  
                  if (c2[0].equals(" phrase1")) {
                      key1 = c2[1];
                  }
                  if (c2[0].equals(" phrase2")) {
                      key = c2[1];
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>LoginActivity should not call <code>finish()</code> (returning to MainActivity) until I've received the keys from the cookies. How can I achieve this, given what I have already? I don't know how LoginActivity can hold off returning to MainActivity until it has the data from its WebView.</p>
  
  <pre><code>public class LoginActivty extends Activity {
      WebView webview;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_login);
          setupViews();
          setContentView(webview);
      }
      public void setupViews() {
          StrictMode.ThreadPolicy policy = new 
          StrictMode.ThreadPolicy.Builder().permitAll().build();
          StrictMode.setThreadPolicy(policy);
          CookieManager.getInstance().setAcceptCookie(true);
          webview = new WebView(this);
          webview.setWebViewClient(new TempWebViewClient());
          webview.getSettings().setJavaScriptEnabled(true);
          webview.loadUrl("website");
      }
  }
  </code></pre>