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
android uiwebview onbackpressed


source to share


No one has answered this question yet

See similar questions:

305
How do I go back to the previous page if the Back button is clicked in the WebView?

or similar:

2609
Is there a unique identifier for an Android device?
687
Android error: Failed to install * .apk on device *: timeout
316
Disable back button in android
8
Android App Development - WebView Not Working
1
How can I prevent the Android web browser from manipulating the web page?
1
Calling Routes Using Google Maps on Android
1
Android WebViewClient setting throws NullPointerException in AsyncTask
0
Android WebView cannot handle pageFinish
0
OnReceivedError is deprecated: "deprecated"
0
how to add webview to scrollable tabs?



All Articles
Loading...
X
Show
Funny
Dev
Pics