Android WebView not working as expected on the back of the device

Hi I am developing android where I am using webview. There are several pages in my web view. So my assigned functionality is like this, when I press the device back button, it should go back to the previous url, and when on the last web page, it should close this activity and go to the previous operation. I tried it in the following way, which works great on newer devices, but doesn't work as expected on older versions of Android. My code looks like this:
     webView = new WebView(this);
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {

                if (url.contains(URLManager.URL_DICOM_BACK_PRESSED_URL)) {
                    processBackPressed();
                    return true;
                } else {
                    return super.shouldOverrideUrlLoading(view, url);
                }


            }


            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                if (failingUrl.contains(URLManager.URL_DICOM_BACK_PRESSED_URL)) {
                    processBackPressed();
                } else {
                    showError("Server unreachable");
                }

            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);

                runOnUiThread(new Thread() {
                    @Override
                    public void run() {
                        super.run();

                        if(!webviewLoaded) {
                            webviewLoaded = true;
                            content.removeAllViews();
                            content.setOrientation(LinearLayout.VERTICAL);
//                            LLM.addInParent(content, textView, -1001, -999, 0);
                            LLM.addInParent(content, webView, -1001, 0, 1);
                        }
                    }
                });
            }
        });
        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl(url);

    @Override
    public void onBackPressed() {
        if (webView != null && webView.canGoBack()) {

            webView.goBack();
        } else {

            processBackPressed();
        }
    }

    public void processBackPressed() {
        super.onBackPressed();
        finish();
    }

      

One newer version of Android works fine on the code, but doesn't work in older versions. What I am observing shouldOverrideUrlLoading

does not work in older versions.

Am I doing something wrong? Help is needed. Thank.

+3


source to share





All Articles