WebView - Update option only appears once in onReceivedError, see more

1

Hello, I'm developing a project for my site, and until then it's getting really cool. But I had a problem and it is as follows, whenever the page of error I called the Snackbar feature, it looks like this:

webView.setWebViewClient(new WebViewClient() {

    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        Snackbar snackbar = Snackbar.make(snackbarCoordinatorLayout, "ERRO NA CONEXÃO", Snackbar.LENGTH_INDEFINITE);
        snackbar.setAction("Atualizar", new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                webView.reload();
            }
        });

        View sbView = snackbar.getView();
        TextView textView = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_text);
        textView.setTextColor(Color.RED);

        snackbar.show();
    }

Until then, okay, the page when the error is the feature is shown, when the user selects "Refresh" and the page continues giving error the feature continues persisting ... But I do not want to leave the original error page , so I created a page in html and called along with Snackbar, thus:

webView.setWebViewClient(new WebViewClient() {

    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        webView.loadUrl("file:///android_asset/error.html");
        Snackbar snackbar = Snackbar.make(snackbarCoordinatorLayout, "ERRO NA CONEXÃO", Snackbar.LENGTH_INDEFINITE);
        snackbar.setAction("Atualizar", new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                webView.reload();
            }
        });

        View sbView = snackbar.getView();
        TextView textView = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_text);
        textView.setTextColor(Color.RED);

        snackbar.show();
    }

But this time the problem comes up, I call this "custom error page" the Snackbar feature appears only once, ie only for the first time the error page, hence when the user selects "Update" the page giving error again it does not appear anymore. That is, this error page is messing up something, can you help me? How do I make both works, and the feature works without problem.

    
asked by anonymous 01.03.2016 / 15:18

1 answer

1

The problem is when you pass the reload() function to WebView , it is reloading your previous error page, not your original page.

Do the following, before loading your custom error page, store the original page in a variable:

String urlOriginal = webView.getUrl();   
webView.loadUrl("file:///android_asset/error.html");

And in your function onClick(View v) you load the original url again:

@Override
public void onClick(View v) {
   webView.loadUrl(urlOriginal);
}

This way the error page will be displayed, and if the user clicks to refresh, the original page will be displayed instead of updating the error page.

    
01.03.2016 / 17:23