gpt4 book ai didi

java - CordovaWebView 搞砸了 android 中的 onBackPressed 方法

转载 作者:行者123 更新时间:2023-11-30 09:06:35 27 4
gpt4 key购买 nike

正如标题所说,CordovaWebViewonBackPressed 在 android 中的组合给出了奇怪的结果。我有混合应用程序。我的主要 Activity 有 DrawerLayoutCordovaWebView。我的 onBackPressed:

 @Override

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

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

最佳答案

我遇到了同样的问题。我的解决方案是从 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com