webView.canGoBack() not working anymore

时间:2017-12-18 08:03:53

标签: android webview onbackpressed

Recently all of my program Project1; uses RegularExpressions; var Text: string; Pattern: string; RegEx: TRegEx; Match: TMatch; begin Text := 'Hello' + #13#10 + '// Test' + #13#10 + 'Text' + #13#10; Pattern := '//[^$]*$'; RegEx := TRegEx.Create(Pattern, [roCompiled, roMultiLine]); Match := RegEx.Match(Text); if (Match.Success) then begin Match.Index; // 8 -> Expected Match.Length; // 15 -> I would like to have 9 end; end. based apps started to have problem with back button. It looks an issue with update of some Android Component, Kindly help with the issue or some alternative solution?

WebView always returning false.

PS: All apps were working fine in previous days.

I'm using it like this:

webView.canGoBack()

1 个答案:

答案 0 :(得分:1)

像这样创建自己的canGoBack ....

ArrayList<String> urls = new ArrayList<>();
urls.add(www.google.com);

//after that you need override this method
 webView.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            Log.d(TAG, "shouldOverrideUrlLoading: "+url);
            urls.add(url);
            return true;
        }
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) 
        {
            super.onPageStarted(view, url, favicon);
            pDialog.show();
        }
        @Override
        public void onPageFinished(WebView view, final String url) {
            pDialog.dismiss();
        }
    });

//for onBackPressed, same like in custom button for back click option
@Override
public void onBackPressed() {
    if(!canGoBack()
    {
       super.onBackPressed();
    }
}

//Custome Method
public boolean canGoBack()
{
    if(urls.size() == 1) // home page
    {
        return false;
    }
    else
    {
        webView.loadUrl(urls.get(urls.size()-2)); // load previous url
        urls.remove(urls.size()-1); // remove last loaded url
        return true;
    }
}