Skip to content Skip to sidebar Skip to footer

Reloading A Webview With A Javascript Call From A Loaded Web Page

I have a WebView in an app that loads a specific web page. On that web page is a button that uses JavaScript to call a method within the Android activity to reload the URL in the W

Solution 1:

Loading a page in a WebView componenet (or reloading) needs to be done on the UI thread for some reason, so simply wrapping the reload call in runOnUiThread resolves this issue.

    @JavascriptInterface
    public void reloadSite(){
        Toast.makeText(mContext, getString(R.string.reloadingWebApp), Toast.LENGTH_LONG).show();
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mWebView = (WebView) findViewById(R.id.activity_main_webview);
                mWebView.loadUrl(getString(R.string.web_app_url));
            }
        });
    }

Post a Comment for "Reloading A Webview With A Javascript Call From A Loaded Web Page"