CordovaWebView弄乱了与Android的onBack pressed方法 [英] CordovaWebView messes up with onBackPressed method in android

查看:984
本文介绍了CordovaWebView弄乱了与Android的onBack pressed方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为标题所说 CordovaWebView onBack pressed android的结合是给奇怪的结果。
我有混合的应用程序。我的主要活动有 DrawerLayout CordovaWebView
我onBack pressed:

As title says CordovaWebView and onBackPressed in android in combination are giving weird results. I have hybrid app. My main activity has DrawerLayout and CordovaWebView. My onBackPressed:

 @Override

public void onBackPressed(){
  if(drawerIsOpen){
    //close drawer
  }else if(webviewIsIn){
     //hide webview
  }else{
     super.onBackPressed();
  }
}

当我使用Android的的WebView 被覆盖的方法如预期调用。当我改变 CordovaWebView 方法甚至不会被调用,而不是原生 onBack pressed 会所谓替代。
我曾尝试重写的onkeydown 的onkeyup ,但它给了我同样的结果,这些方法只是没有被调用。
我使用的科尔多瓦2.9.0和测试设备的Galaxy Note 2的Andr​​oid 4.2.2软糖
DrawerLayout 对我只是禁用回pressed功能关闭。
我希望你们能理解这个问题。

When I use android's WebView the overridden method is called as expected. And when I change to CordovaWebView the method wouldn't even get called, instead native onBackPressed would be called instead. I have tried overriding onKeyDown and onKeyUp but it gives me the same result, the methods are just not being called. I'm using Cordova 2.9.0 and testing device is Galaxy Note 2, Android jellybean 4.2.2 DrawerLayout has the close on back pressed functionality I've just disabled it. I hope you guys can understand the problem.

推荐答案

我遇到了同样的问题。我的解决办法是从 CordovaWebView 派生并重写公共布尔的onkeyup(INT键code,KeyEvent的事件)与像这样(为科尔多瓦3.4.0中,code是 CordovaWebView.onKeyUp(INT,KeyEvent的一部分)

I encountered the same issue. My solution was to derive from CordovaWebView and override public boolean onKeyUp(int keyCode, KeyEvent event) with something like this (for Cordova 3.4.0, the code is a part of the CordovaWebView.onKeyUp(int, KeyEvent)):

public class CustomCordovaWebView extends CordovaWebView {

    protected View mCustomView;

    protected boolean bound;

    public CustomCordovaWebView(final Context context) {
        super(context);
    }

    public CustomCordovaWebView(final Context context, final AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomCordovaWebView(final Context context, final AttributeSet attrs, final int defStyle) {
        super(context, attrs, defStyle);
    }

    @TargetApi(11)
    public CustomCordovaWebView(final Context context, final AttributeSet attrs, final int defStyle, final boolean privateBrowsing) {
        super(context, attrs, defStyle, privateBrowsing);
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        // If back key
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            // A custom view is currently displayed  (e.g. playing a video)
            if (mCustomView!=null){
                this.hideCustomView();
            }else{
                // The webview is currently displayed
                // If back key is bound, then send event to JavaScript
                if (this.bound) {
                    this.loadUrl("javascript:cordova.fireDocumentEvent('backbutton');");
                    return true;
                } else {
                    // If not bound
                    // Go to previous page in webview if it is possible to go back
                    if (this.backHistory()) {
                        return true;
                    }
                    // If not, then invoke default behavior
                    else {
                        //this.activityState = ACTIVITY_EXITING;
                        //return false;
                        // If they hit back button when app is initializing, app should exit instead of hang until initialization (CB2-458)
                        // this.cordova.getActivity().finish();
                        // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this thing is closing your activity in CordovaWebView
                    }
                }
            }
        } else {
            return super.onKeyUp(keyCode, event);
        }

        return false;
    }

    @Override
    public void hideCustomView() {
        mCustomView = null;
        super.hideCustomView();
    }

    @Override
    public void showCustomView(final View view, final WebChromeClient.CustomViewCallback callback) {
        mCustomView = view;
        super.showCustomView(view, callback);
    }

    @Override
    public void bindButton(final boolean override) {
        bound = override;
        super.bindButton(override);
    }
}

如果有一个更好的解决方案,我会感兴趣。

If there is a better solution, I would be interested in it.

这篇关于CordovaWebView弄乱了与Android的onBack pressed方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆