WebView does not render local HTML page on app launch in Android 4.0.x Ice Cream Sandwich
Asked Answered
M

3

34

I know this question has been asked in different formats on this forum, but none of the answers - accepted or otherwise that I found there have helped me so far.

I am working on a hybrid app that uses native android, HTML and Adboe's Flex SDK (without any frameworks like PhoneGap etc., straightforward code using Android's own WebView).

Here's the issue I am facing:

When the app is launched, at first the activity "AppEntry" for the flex sdk is triggered, it is a blank activity which simply sets the context and initial setups for the flex SDK. Next, the native MainActivity is launched which uses a WebView to load the HTML project. On Android 4.0.x (ICS), the webview turns out to be blank(white) even thought the URL is loaded (onPageFinished() is called successfully for the URL in question). This happens the first time the app is installed and launched, after stopping the app (by removing it from the recent apps bar), the page is loaded as expected. Repeated re-launching like this reproduces the issue sometimes but with unpredictable frequency.

Some things for consideration:

  1. Due to flex sdk constraints, these HTML files cannot be stored directly in the '/assets' folder but a directory structure contained within the assets folder.

  2. Issue occurs only on Ice Cream Sandwich (known issue I guess)!

Stuff that I have tried already:

  1. Hardware acceleration is off (on/off does not matter, tested with both)

2.

WebSettings settings = webView.getSettings();        
        settings.setJavaScriptEnabled(true);
        settings.setUseWideViewPort(true);
        settings.setLoadWithOverviewMode(true);
        settings.setSupportMultipleWindows(true);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        settings.setLoadsImagesAutomatically(true);
        settings.setDomStorageEnabled(true);
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        settings.setSaveFormData(true);
        settings.setAllowFileAccess(true);
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
            settings.setAllowContentAccess(true);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                settings.setAllowFileAccessFromFileURLs(true);
                settings.setAllowUniversalAccessFromFileURLs(true);
            }
        }
        settings.setAppCacheEnabled(true);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        settings.setLoadsImagesAutomatically(true);
        boolean enableZoom = true;
        settings.setBuiltInZoomControls(enableZoom);
        settings.setSupportZoom(enableZoom);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ECLAIR_MR1) {
            initializePluginAPI(webView);
        }
        settings.setDatabaseEnabled(true);

The issue is killing my app's review comments on the play store. Any help or insight would be appreciated.

Macrospore answered 28/8, 2015 at 9:0 Comment(5)
I'm not sure how it's possible to mix Android with AIR and Flex SDK but you could try using the AIR html control and then embedding the HTML file and then creating a string from that dynamically and setting the htmlText of the webview or use loadString if using an HTML loader instance.Delate
It's not actually just a single HTML page but a whole HTML application. It works as expected for versions except Android 4.0.x . Therefore I am looking for a fix for 4.0 (ICS).Macrospore
Try to load the webview 500 miliseconds (or something comparable) later using a postDelayed call. This looks like a load / synchronisation issue, loading the webcontent a bit later might solve it.Xiphoid
settings.setPluginsEnabled(true); ?Lobbyist
I think, you might be able to give it a shot to Custom Tabs: android-developers.blogspot.com/2015/09/… , I hope that works better for you.Dukes
S
1

the webview turns out to be blank(white) even thought the URL is loaded (onPageFinished() is called successfully for the URL in question).

I solved it by use mWebView.post()

Snook answered 4/12, 2015 at 10:12 Comment(0)
S
0

If its only above 4.0 than i would recommend you to add a permission to your application as Android needs this to load assets file else behavior is intermittent.

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Stralsund answered 13/10, 2015 at 11:41 Comment(0)
G
0

In Android 3.x - 4.0.x, WebView has a very critical bug on loading local HTML when url containing query parameter(?) or anchor(#). This problem is not fixed until Android 4.1. You should apply custom WebView and WebViewClient. Here is the fixed WebView and WebViewClient.
You should use WebViewEx and WebViewClientEx instead of WebView and WebViewClient, and also override WebViewClientEx like below. This is Android project member's workaround.

webViewEx.setWebViewClient(new WebViewClientEx(getContext()) {
    @Override
    public boolean shouldOverrideUrlLoadingEx(WebView view, String url) {
        boolean redirected = super.shouldOverrideUrlLoadingEx(view, url);

        if (!redirected) {
            if (url != null && URLUtil.isHttpUrl(url) || URLUtil.isHttpsUrl(url)) {
                view.getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
                redirected = true;
            }
        }
        return redirected;
    }

    @Override
    public WebResourceResponse shouldInterceptRequestEx(WebView view, String url) {
        InputStream stream = inputStreamForAndroidResource(url);
        if (stream != null) {
            return new WebResourceResponse(null, null, stream);
        }
        return super.shouldInterceptRequestEx(view, url);
    }

    private InputStream inputStreamForAndroidResource(String url) {
        final String ANDROID_ASSET = "file:///android_asset/";

        if (url.startsWith(ANDROID_ASSET)) {
            url = url.replaceFirst(ANDROID_ASSET, "");
            try {
                AssetManager assets = getContext().getAssets();
                Uri uri = Uri.parse(url);
                return assets.open(uri.getPath(), AssetManager.ACCESS_STREAMING);
            } catch (IOException e) {
            }
        }
        return null;
    }
});
Granicus answered 14/7, 2016 at 6:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.