Home » Android » apache flex – WebView does not render local HTML page on app launch in Android 4.0.x Ice Cream Sandwich

apache flex – WebView does not render local HTML page on app launch in Android 4.0.x Ice Cream Sandwich

Posted by: admin April 23, 2020 Leave a comment

Questions:

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.

How to&Answers:

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()

Answer:

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" />

Answer:

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;
    }
});