WebView canGoBack()始终返回false

时间:2013-05-06 10:21:42

标签: android android-webview

我对WebView进行了细分,在我的活动中,我覆盖了onKeyDown

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    Log.i("mxwebviewact", "mxwebactivity onKeyDown...");

    if (keyCode == KeyEvent.KEYCODE_BACK) {
        if(webView.canGoBack()){
            Log.i("mxwebviewact", "webview can go back");
            webView.goBack();
            return false;
        }else{
            Log.i("mxwebviewact", "webview cannot go back");
        }

    }

    return super.onKeyDown(keyCode, event);
}

问题是webView.canGoBack()总是返回false,我不能使用后退按钮返回。

在我的WebView类中,我重写了setWebViewClient

public void setWebViewClient(MobileBankWebViewClient client){
    if(client == null){
        return;
    }

    super.setWebViewClient(webViewClient);
}

    // constructor
public MXWebView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
}

public MXWebView(Context context, AttributeSet attrs) {
    super(context, attrs);

    init();
}

public MXWebView(Context context) {
    super(context);
    init();
}

private void init(){
    this.setWebViewClient(new MobileBankWebViewClient());

    webChromeClient = new MobileBankWebChromeClient();        
    this.setWebChromeClient(webChromeClient);       

    this.setScrollBarStyle(0);

    WebView.enablePlatformNotifications();
    this.requestFocus();
    this.setScrollbarFadingEnabled(true);
    this.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);

    WebSettings setting = this.getSettings();
    setting.setCacheMode(WebSettings.LOAD_DEFAULT);
    setting.setJavaScriptEnabled(true);
    setting.setJavaScriptCanOpenWindowsAutomatically(true);
    setting.setPluginsEnabled(false);
    setting.setDefaultTextEncodingName("UTF-8");
    setting.setSupportZoom(false);
    setting.setBuiltInZoomControls(false);
    setting.setUseWideViewPort(true);
    setting.setDomStorageEnabled(true);
    setting.setAppCacheMaxSize(8*1024*1024);
    setting.setAllowFileAccess(true);
    setting.setAppCacheEnabled(true);
    setting.setDatabaseEnabled(true);
    setting.setLightTouchEnabled(true);
    setting.setSavePassword(false);
    setting.setSaveFormData(false);
    String dir = this.getContext().getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath();
    setting.setDatabasePath(dir);
    setting.setGeolocationDatabasePath(dir);
    setting.setGeolocationEnabled(true);

    try {
        Method setLayerTypeMethod = this.getClass().getMethod("setLayerType", new Class[] {int.class, Paint.class});
        setLayerTypeMethod.invoke(this, new Object[] {1, null});
        System.out.println("android version >= 3.0");
    } catch (NoSuchMethodException e) {
        // Older OS, no HW acceleration anyway
        System.out.println("android version < 3.0");

    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    }   

    Log.i("xxx", "mxwebview finish init");
}

在MobileBankWebViewClient中,我覆盖了shouldOverrideUrlLoading

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
    if(url != null){
        url = url.trim();
    }else{
        return true;
    }
    try{
        view.loadUrl(url);
        return true;

        // some other treatment...
    }catch(Exception e){
        e.printStackTrace();
    }

    return true;
}

我无法找到错误的位置,请帮助我。

0 个答案:

没有答案