Open liuyi opened 4 years ago
At Cocos2dxWebView.java,this.setFocusableInTouchMode(true); This line will cause the app crash on android with cocos2dx 3.17.2
Finally, I resolved this issue. The reason is that Cocos2dxGLSurfaceView haven't get focus when WebView removed from stage or hidden.
How to fix:
public static void removeWebView(final int index) { sCocos2dxActivity.runOnUiThread(new Runnable() { @Override public void run() { Cocos2dxWebView webView = webViews.get(index); if (webView != null) { webViews.remove(index); sLayout.removeView(webView); }
//add this code to fix focus issue
boolean isHaveVisibleWebView = false;
for (int i = 0; i < webViews.size(); i++) {
if (webViews.get(i).getVisibility() == View.VISIBLE) {
isHaveVisibleWebView = true;
break;
}
}
if (!isHaveVisibleWebView) {
Cocos2dxGLSurfaceView.getInstance().requestFocus();
}
}
});
}
public static void setVisible(final int index, final boolean visible) {
sCocos2dxActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
Cocos2dxWebView webView = webViews.get(index);
if (webView != null) {
webView.setVisibility(visible ? View.VISIBLE : View.GONE);
}
//add this code to fix focus issue
if(webView.getVisibility()==View.GONE){
Cocos2dxGLSurfaceView.getInstance().requestFocus();
}
}
});
}
Steps to Reproduce:
`testWeb:function(){ var _self=this; var rect = cc.visibleRect;
}`
Tap the back button of device, or slide in from right side on infinity display phone.
after a while, the webview removed, do step 3.
Found a same post at:https://github.com/cocos2d/cocos2d-x/issues/20521
Another post with a solution(NOT TOO GOOD ,BUT WORKS): https://github.com/cocos2d/cocos2d-x/pull/18153