title: Android WebView, load from cache and attempt to refresh
body: <p>I am trying to create a small app that loads a webpage and stores it in the cache. When swiping refresh it must attempt to update, but if there is no internet access it should keep the current version.</p>
  
  <p>Currently, I've got "swipe to force refresh" and "load from the cache" working, but can not seem to figure out how to "attempt to refresh if possible".</p>
  
  <pre><code>swipeRefreshLayout = findViewById(R.id.swipe);
  swipeRefreshLayout.setOnRefreshListener(
          new SwipeRefreshLayout.OnRefreshListener() {
              @Override
              public void onRefresh() {
                  myWebView.clearCache(true);
                  myWebView.reload();
              }
          }
  );
  
  WebSettings webSettings = myWebView.getSettings();
  webSettings.setJavaScriptEnabled(true);
  webSettings.setAppCacheEnabled(true);
  webSettings.setAppCachePath(getBaseContext().getCacheDir().getPath());
  webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
  
  myWebView.loadUrl(config.getString("website_root"));
  myWebView.setWebViewClient(new WebViewClient() {
      @Override
      public void onPageStarted(WebView view, String url, Bitmap favicon) {
          if (!swipeRefreshLayout.isRefreshing()) {
              swipeRefreshLayout.setRefreshing(true);
          }
      }
  
      @Override
      public void onPageFinished(WebView view, String url) {
          swipeRefreshLayout.setRefreshing(false);
      }
  });
  </code></pre>