Java CordovaWebView在android中使用OnBackpress方法时出错
正如标题所说,android中的Java CordovaWebView在android中使用OnBackpress方法时出错,java,android,cordova,Java,Android,Cordova,正如标题所说,android中的CordovaWebView和onBackPressed组合在一起会产生奇怪的结果。 我有混合应用程序。我的主要活动有DrawerLayout和CordovaWebView。 我的回答是: @Override public void onBackPressed(){ if(drawerIsOpen){ //close drawer }else if(webviewIsIn){ //hide webview }else{
CordovaWebView
和onBackPressed
组合在一起会产生奇怪的结果。
我有混合应用程序。我的主要活动有DrawerLayout
和CordovaWebView
。
我的回答是:
@Override
public void onBackPressed(){
if(drawerIsOpen){
//close drawer
}else if(webviewIsIn){
//hide webview
}else{
super.onBackPressed();
}
}
当我使用android的WebView
时,会按预期调用重写的方法。当我切换到CordovaWebView
时,甚至不会调用该方法,而是会调用nativeonBackPressed
。
我尝试过重写onKeyDown
和onkeydup
,但它给出了相同的结果,只是没有调用这些方法。
我使用的是Cordova 2.9.0,测试设备是Galaxy Note 2,Android jellybean 4.2.2
DrawerLayout
具有关闭后按功能,我刚刚禁用了它。
我希望你们能理解这个问题 我遇到了同样的问题。我的解决方案是从
CordovaWebView
派生,并用类似的内容覆盖公共布尔onKeyUp(int-keyCode,KeyEvent事件)
(对于Cordova 3.4.0,代码是CordovaWebView.onKeyUp(int,KeyEvent)
):
如果有更好的解决方案,我会对它感兴趣。遗憾的是,这个答案并不能解决我的问题。我的代码片段是针对Cordova 3.4.0的,所以简单的重用可能在2.9.0上不起作用。请尝试查看CordovaWebView的2.9.0源代码,以适应这种方法。以后我可以自己检查,但无论如何我可能没有机会测试它。我确实根据需要进行了更改。有趣的是,在CordWeb视图上调用clearHistory时,bug似乎并不经常出现。感谢您的帮助。不再支持5.1.1,因为CordovaWebView是一个接口,不再是类。
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);
}
}