How to make a WebView save the web pages it loads?

0

I have an Android application in which a webView loads 3 different webs depending on 3 buttons in the interface of the application, I would like that when pressing another of the buttons if the page has been loaded before it does not reload

I've already tried using

mWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
                     mWebView.getSettings().setAppCacheMaxSize(8 * 1024 * 1024); 
                    mWebView.getSettings().setAppCachePath(getApplicationContext().getCacheDir().getAbsolutePath());
                    mWebView.getSettings().setAllowFileAccess(true);
                    mWebView.getSettings().setAppCacheEnabled(true);
                    mWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT); 
                    if (!isNetworkAvailable()) { 
                    mWebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
                    }

    
asked by masterfabela 26.09.2016 в 10:05
source

2 answers

1

I'll give you an example that worked for me a long time ago, this WebView showed an image of a site (Thanks to an answer from @Elenasys that I mixed code of it and others in S.O: p)

 webview = (WebView) rootView.findViewById(R.id.loadImagePortada);

        webview.getSettings().setAppCacheMaxSize(5 * 1024 * 1024); // 5MB
        webview.getSettings().setAppCachePath(getActivity().getCacheDir().getAbsolutePath());
        webview.getSettings().setAllowFileAccess(true);
        webview.getSettings().setAppCacheEnabled(true);
        webview.getSettings().setJavaScriptEnabled(true);
        webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
        webview.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);

        if ( !isNetworkAvailable() ) { // Cargando sin conexión
            webview.getSettings().setCacheMode( WebSettings.LOAD_CACHE_ONLY );

        }
        final AlertDialog dialogoAlerta = new AlertDialog.Builder(getActivity()).create();

        progressBar = ProgressDialog.show(getActivity(), "Cargando portada", "Espere por favor...");

        webview.setWebViewClient(new WebViewClient() {

            public boolean shouldOverrideUrlLoading(WebView view, String url) {


                view.loadUrl(url);

                return true;
            }

            public void onPageFinished(WebView view, String url) {

                if (progressBar.isShowing()) {
                    progressBar.dismiss();
                }
            }

            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                dialogoAlerta.setTitle("Error");
                dialogoAlerta.setMessage(description + " Intente mas tarde por favor.");
                dialogoAlerta.setButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        return;
                    }
                });
                dialogoAlerta.show();
            }
        });


        webview.loadUrl("http://mispruebas.eshost.com.ar/getimage.php");

 private boolean isNetworkAvailable() {
        ConnectivityManager connectivityManager
                = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        return activeNetworkInfo != null;
    }

The first time I opened the activity I loaded from the web, when I left that activity and later came back to enter and I did not download it, loaded from the cache.

It's a very functional example;)

    
answered by 26.09.2016 / 16:56
source
2

Each of the 3 buttons performs the loading of a different URL in a WebView, if you want to save the cache use the method setCacheMode() of android.webkit.WebSettings using the constant

  

LOAD_CACHE_ELSE_NETWORK Use cached resources when available, even if they have expired.

Example:

myWebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);

As another option you could use the constant LOAD_CACHE_ONLY :

  

LOAD_CACHE_ONLY Do not use the network, load cache.

    
answered by 26.09.2016 в 10:21