Android: WebView stops loading after several pages
Asked Answered
F

0

16

I have a webview I am loading custom HTML into using loadDataWithBaseURL:

mArticleWebView.loadDataWithBaseURL("http://www.example.com/", htmlString, "text/html", "utf-8", null);

This will work for several pages, and then events stop firing in the WebView for seemingly no reason in Jelly Bean on tablets and the web's view will be blank.

This this the WebViewCreation Code:

private void prepareWebView() {
mArticleWebView.setBackgroundColor(getResources().getColor(R.color.white));
mArticleWebView.setScrollBarStyle(WebView.SCROLLBARS_INSIDE_OVERLAY);
mArticleWebView.addJavascriptInterface(new ArticleJavaScriptInterface(getActivity()), "Company");
mArticleWebView.setWebChromeClient(new CustomChromeClient());
/* WebViewClient must be set BEFORE calling loadUrl! */
mArticleWebView.setWebViewClient(new WebViewClient() {
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favi) {
        Log.e(TAG, "Signs of life, shock and awe.");
        super.onPageStarted(view, url, favi);
    }

  @Override
  public void onPageFinished(WebView view, String url) {
    Log.e(TAG, "onPageFinished..." + url);
    super.onPageFinished(view, url);
  }

  @Override
  public boolean shouldOverrideUrlLoading(WebView view, String url) {
    if (url != null && (url.startsWith("http://") || url.startsWith(Constants.MEDIA_ENTRY_PROTOCOL + "://"))) {

        view.getContext().startActivity(
          new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
      return true;
    } else {
      return false;
    }
  }
});


WebSettings webSettings = mArticleWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
if (BuildConfig.DEBUG) {
  Log.d(TAG, "User Agent is " + webSettings.getUserAgentString());
}

 }

The specific problem is onPageStarted and the other overrides just stop getting called after a while. All the other code is reached, and the HTML generated looks fine.

Invalidating, reloading, re-initializing does not seem to do any good. It just dies and stays dead.

What else can I try? What else might be causing issues?

------------EDIT----------

After hours and hours of head bashing, and then discovering it is doing this outside of the pager as well, I am 90% certain I am hitting the issue linked below. That being said, I do not have a fix yet if anyone has any suggestions.

AdView bug causing all webviews stop loading when orientation changes and multi-threads are running?

----- FINAL EDIT ------- This never got 'fixed', we now load the ads via HTML rather than natively for Jelly Bean.

It is not:

Anything to do with the ViewPager. Interstitial ads would also cause it eventually. Most of our ads were just in the ViewPager.

A conflict between ActionBarSherlock. Updating to AppCompat didn't help anything.

A problem with just WebView. Strange behavior was also noticed in XWalk, including trying to load the ad in itself and then having flickering, scary weirdness, although XWalk would recover after a bit. This was with both ActionBarSherlock and AppCompat.

My problem has nothing to do with rotation; I'm blaming threading in general. We do have a fair bit of threading going on.

We had an old version of Google Ads before (6.21). I updated to Google Play 7 before I even pointed my finger at ads causing the problem since it would be required. There was no behavior change in the update. AKA: PublisherAdView and DfpAdView both react the same.

I am wondering if something happens to move the project to Android Studio, but have no idea what to point at.

At the end of the day this is an OS issue, but I don't know what in particular tickled it. I am very interested in hearing from anyone going through the same thing.

Flickinger answered 21/4, 2015 at 14:21 Comment(11)
what happens if you just use webView.loadUrl without the base url things?Joyann
loadURL is very unhappy being fed raw html, loadData behaves the same, loadDataWithBaseURL with a blank base uRL behaves the sameFlickinger
oh. well that's true, but then what exactly is "http://www.example.com/"? Is it local resource? Is it a website? What's in the extra HTML? This is odd. Does the website use jQuery Mobile?Joyann
It is my company's URL and as far as I can tell, throwing that in there does nothing, but that's how the code came to me. The site does not use jQuery mobile, and I'm pretty sure it doesn't use jQuery even, but I'll double check. The extra HTML is an article, so it's mostly just text.Flickinger
I was asking because using jQuery Mobile and $.changePage('other.html', {changeHash: true}) on non-SPA websites makes the Android back button not work about 3 pages deep, but I guess that is not relevant here. If having to load HTML like that directly is unavoidable, then I'm out of ideas for the moment.Joyann
Since I am in a hit it with a hammer until it gives me a hint mode, I swiched back to loadURL. It is, of course unhappy, and it STILL has the same errors and goes blank after a while.Flickinger
@Flickinger does it happen only in 4.1 or other devices?Wolsky
I am having trouble with Nexus 10 API 16, works in Galaxy Nexus API 21, and Nexus 4 API 15, and Nexus One API 19.Flickinger
An extreme idea I can recommend is that for 4.0+, replace your WebView with XWalkWebView. But it greatly increases application size...Joyann
Hi @orangeberri07¿did you found a solution for this? I think I'm facing same problem here :-(Byproduct
@Byproduct No. I instead switched from using native libraries to using JavaScript and HTML to load ads.Flickinger

© 2022 - 2024 — McMap. All rights reserved.