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

Asked

Viewed 242 times

1

Hi, I’m developing a project for my site, and until then it’s getting really cool. Only I had a little problem and it’s like this, whenever the page gives error I called Feature Snackbar, getting 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 error the Feature is shown, when the user selects "Refresh" and the page continues giving error Feature continues persisting ... Only I don’t want to leave the original error page, so I created an html page and called along with Snackbar, getting like this:

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();
    }

Only at this time comes the problem, I calling this "custom error page" the Snackbar Feature appears only once, or it is only for the first time that the error page, then when the user selects "Update" even the page giving error again it no longer appears. I mean, this page of error is getting in the way of something, can you help me? How do I make the two work, and the Feature works without problem.

1 answer

1


The problem is when you pass the function reload() to the WebView, it is reloading your error page that you have previously passed, 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 job 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 in case the user click to refresh, the original page will be shown instead of refreshing the error page.

  • Peeeeeeeeeerfeito was exactly that friend. MUUITO obliged.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.