Android - WebView canGoBack()总是返回false?

时间:2017-12-26 12:52:48

标签: java android webview

为什么WebView canGoBack()总是返回false?

if (myWebView.canGoBack()) 
       { myWebView.goBack(); } //always false

的onCreate:

        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setBuiltInZoomControls(true);
        myWebView.loadUrl( getString(R.string.base_url) );
        myWebView.setWebViewClient(new MyWebViewClient(progress));

MyWebviewClient:

public class MyWebViewClient extends WebViewClient {
    private ProgressBar progressBar;

    public MyWebViewClient(ProgressBar progressBar) {
        this.progressBar=progressBar;
        progressBar.setVisibility(View.VISIBLE);
    }

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        progressBar.setVisibility(View.VISIBLE);
        share_btn.setVisibility(View.GONE);
        back_btn.setVisibility(View.GONE);
        super.onPageStarted(view, url, favicon);
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
        // TODO Auto-generated method stub
        progressBar.setVisibility(View.GONE);
        //hide loading image
        findViewById(R.id.imageLogo).setVisibility(View.GONE);
        //show webview
        findViewById(R.id.websiteView).setVisibility(View.VISIBLE);
        toolbar.setVisibility(View.VISIBLE);
        currentURL = url.replace("mapp.","");
        if(url.contains("article")) {
            share_btn.setVisibility(View.VISIBLE);
            back_btn.setVisibility(View.VISIBLE);
        }
    }

    @SuppressWarnings("deprecation")
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (!Global.IsInternetConnected(MainActivity.this))
            Toast.makeText(getApplicationContext(), "No internet connection!", Toast.LENGTH_SHORT).show();
        else
            view.loadUrl(url);
        return true;
    }

    @TargetApi(Build.VERSION_CODES.N)
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
        if (!Global.IsInternetConnected(MainActivity.this))
            Toast.makeText(getApplicationContext(), "No internet connection!", Toast.LENGTH_SHORT).show();
        else {
            view.loadUrl(request.getUrl().toString());
        }
        return true;
    }
}

它在模拟器pixel_XL_API_25上工作正常,但 Samsung S6 Note3 都不能正常工作。

1 个答案:

答案 0 :(得分:1)

最后我找到了解决方案,问题出在了shouldOverrideUrlLoading

请在下面找到正确的shouldOverrideUrlLoading函数:

@SuppressWarnings("deprecation")
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (!Global.IsInternetConnected(MainActivity.this)) {
            Toast.makeText(getApplicationContext(), "No internet connection!", Toast.LENGTH_SHORT).show();
            return true;
        }
        else
            return super.shouldOverrideUrlLoading(view, url);
    }

    @TargetApi(Build.VERSION_CODES.N)
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
        if (!Global.IsInternetConnected(MainActivity.this)) {
            Toast.makeText(getApplicationContext(), "No internet connection!", Toast.LENGTH_SHORT).show();
            return true;
        }
        else 
            return super.shouldOverrideUrlLoading(view, request);
    }