java - CordovaWebView 与 android 中的 onBackPressed 方法混淆

标签 java android cordova

正如标题所说,Android 中的 CordovaWebViewonBackPressed 结合起来会产生奇怪的结果。 我有混合应用程序。我的主要 Activity 有 DrawerLayoutCordovaWebView。 我的 onBackPressed:

 @Override

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

当我使用 android 的 WebView 时,会按预期调用重写的方法。当我更改为 CordovaWebView 时,该方法甚至不会被调用,而是会调用 native onBackPressed 。 我尝试过重写 onKeyDown 和 onKeyUp ,但它给了我相同的结果,只是没有调用这些方法。 我使用的是 Cordova 2.9.0,测试设备是 Galaxy Note 2,Android jellybean 4.2.2 DrawerLayout 具有关闭后按功能,我刚刚禁用了它。 我希望你们能够理解这个问题。

最佳答案

我遇到了同样的问题。我的解决方案是从 CordovaWebView 派生并覆盖public boolean onKeyUp(int keyCode, KeyEvent event)像这样的东西(对于 Cordova 3.4.0,代码是 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);
    }
}

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

关于java - CordovaWebView 与 android 中的 onBackPressed 方法混淆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24400408/

相关文章:

android - 工具栏 ActionMode 背景预 Lollipop 不起作用

cordova - cordova 3.5用于分析的插件

javascript - 轻扫面漆侧菜单

java - 从 web.xml 文件(CDATA 项)中删除 "error"标记

java - 没有 JDK,我怎么能只用 JRE 编译和运行 Java 程序?

Android - 从服务器上的文件夹下载所有文件

ios - Phonegap 3.0.0 - Facebook SDK 3.6 - FB 插件 : Facebook connect not found

java - 如何获取Jsoup中特定html元素的内容?

java - 使用 Dropbox Java API 将文件上传到 Dropbox

java - 在 Facebook 上拉取、发布和更新