WebView 重定向无法返回问题

webview加载重定向页面时候,会出现返回不了上一页的情况。
hitTestResult==null解决重定向问题

public class MainActivity extends Activity {

private Button mRefreshButton;
private WebView mWebView;

public void onCreate(Bundle savedInstance) {
    super.onCreate(savedInstance);
    setContentView(R.layout.main);

    mWebView = (WebView) findViewById(R.id.webview);
    mRefreshButton = (Button) findViewById(R.id.refresh);

    mRefreshButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
             mWebView.reload();
        }
    });

    WebSettings webSettings = mWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);

    mWebView.setWebViewClient(new WebViewClient() {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
          WebView.HitTestResult hitTestResult = view.getHitTestResult();
                //hitTestResult==null解决重定向问题
                if (!TextUtils.isEmpty(url) && hitTestResult == null) {
                    view.loadUrl(url);
                    return true;
                }
             return super.shouldOverrideUrlLoading(view, url);
        }
    });

    mWebView.loadUrl("http://www.gap.com/");
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if(event.getAction() == KeyEvent.ACTION_DOWN) {
        switch(keyCode) {
            case KeyEvent.KEYCODE_BACK:
                if(mWebView.canGoBack()){
                    mWebView.goBack();
                    return true;
                }
                break;
        }

    }
    return super.onKeyDown(keyCode, event);
}
}

你可能感兴趣的:(WebView 重定向无法返回问题)