Anubhav Anand
Anubhav Anand

Reputation: 66

WebView content partially disappears after switching activity and coming back

My WebView loads HTML string with few Images, Texts, and CSS. I click on the image, and it takes me to other activity (say ImageActivity) with a list of images in webview. After I click the back button, some of the texts and images disappear from the webview. See Image

I am Using Gson to parse HTML.

Here is my webview code

    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setDomStorageEnabled(true);
    webView.getSettings().setBuiltInZoomControls(false);
    webView.addJavascriptInterface(new WebAppInterface(this,images), "Android");
    webView.setWebViewClient(new WebViewClient(){
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if(url.contains(Config.SITE_URL)){
                Log.e("WebView","Site URL Clicked");
                return true;
            }else{
                Log.e("WebView","External URL Clicked");
                return true;
            }
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            progressBar.setVisibility(View.GONE);
            webView.setVisibility(View.VISIBLE);
            linearLayout.setVisibility(View.VISIBLE);
        }

        @Override
        public void onReceivedSslError(WebView view, final SslErrorHandler handler, SslError error) {
            handler.proceed();
        }
    });
    .
    .
    .
    html = "<link rel=\"stylesheet\" href=\"post.css\" type=\"text/css\"/>"+content+"<script type=\"text/javascript\">function imgclicked(toast) {Android.showToast(toast);}</script>";
    webView.loadDataWithBaseURL("file:///android_asset/", html, "text/html", "UTF-8", null);

Nothing in logcat

Upvotes: 1

Views: 1111

Answers (1)

Anubhav Anand
Anubhav Anand

Reputation: 66

Solved it myself. It was a silly mistake. Adding proper onPause and onResume worked for me.

@Override
public void onPause() {
    super.onPause();
    webView.onPause();
}

and

@Override
protected void onResume() {
    super.onResume();
    webView.onResume();
}

Upvotes: 1

Related Questions