androidwebviewonbackpressedcustom-pages

webView reloading to custom error page instead of home page


I am currently working on a web app.

I made a custom internet error page by overriding default No internet connection page. The code works fine, after internet connection restores webView loads the homepage but,

Issues: After loading homepage if I press back button, the app again shows custom Internet Error page.

code of webView:-

'''

    private void webview() {
    progressBar = findViewById(R.id.progressBar);
    wv1 = (WebView) findViewById(R.id.webView);

    wv1.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
    wv1.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
    wv1.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
    wv1.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
    wv1.getSettings().setAppCacheEnabled(true);
    wv1.getSettings().setSaveFormData(true);
    wv1.clearCache(false);

    wv1.getSettings().setJavaScriptEnabled(true);
    wv1.getSettings().setDomStorageEnabled(true);
    wv1.getSettings().setDatabaseEnabled(true);
    wv1.getSettings().setAllowFileAccess(true);
    wv1.getSettings().setAllowContentAccess(true);
    wv1.getSettings().setUseWideViewPort(true);
    wv1.getSettings().setLoadWithOverviewMode(true);

    wv1.getSettings().setMinimumFontSize(1);
    wv1.getSettings().setMinimumLogicalFontSize(1);
    wv1.setInitialScale(1);

    wv1.setWebViewClient(new WebViewClient(){

        @Override
        public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
            if (!isNetworkConnected()){
                loadCustomUrl();
            }else {
                super.onReceivedError(view, request, error);
            }
        }

        @Override
        public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
            handler.proceed();
        }
    });

    wv1.setWebChromeClient(new WebChromeClient(){

        @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
        public boolean onShowFileChooser(WebView mWebView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams)
        {
            if (uploadMessage != null) {
                uploadMessage.onReceiveValue(null);
                uploadMessage = null;
            }

            uploadMessage = filePathCallback;

            Intent intent = fileChooserParams.createIntent();
            try
            {
                startActivityForResult(intent, 100);
            } catch (ActivityNotFoundException e)
            {
                uploadMessage = null;
                Toast.makeText(getApplicationContext(), "Cannot Open File Chooser", Toast.LENGTH_LONG).show();
                return false;
            }
            return true;
        }
    });
    loadHomeUrl();

}'''

Overriding onBackPressed method:-

'''

    @Override
    public void onBackPressed() {
    if (wv1.canGoBack()) {
        wv1.goBack();
    } else {
        if (doubleBackToExitPressedOnce) {
            super.onBackPressed();
            return;
        }

        this.doubleBackToExitPressedOnce = true;
        Toast.makeText(this, "press BACK again to exit", Toast.LENGTH_SHORT).show();

        new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {
                doubleBackToExitPressedOnce = false;
            }
        }, 2000);
    }
}'''

Solution

  • I solved my issue:

    I made another layout instead of creating an HTML file and loading it in webView. So now whenever my function confirms loss of internet connection, I just set the visibility of my custom error layout to VISIBLE.