Long Dao
Long Dao

Reputation: 1371

How to prevent webview to reload webpage in Android

I got a webview which is implemented like that:

this.webView = (WebView) view.findViewById(R.id.typeform);
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDomStorageEnabled(true);
        webView.setWebViewClient(new WebViewClientHelper(activity));

I don't want to reload the webpage if I click back button and then come to the fragment again, so I implemented that:

In the onCreateView:

if (webViewBundle == null) {
            webView.loadUrl(Constants.TYPEFORM_URL);
        } else {
            webView.restoreState(webViewBundle);
        }

In the onPause():

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

        webViewBundle = new Bundle();
        webView.saveState(webViewBundle);
    }

Not sure why it does not work. Any recommendation?

Thanks in advance.

Upvotes: 0

Views: 1076

Answers (1)

yakobom
yakobom

Reputation: 2711

If you are getting to onCreateView when you click the back button, it means you are creating a new view instance every time. You need to have one instance, and if you want to override the behavior when you come back to it, implement onResume.

Upvotes: 1

Related Questions