问题
I use below code to go back in webview at first try. But for the low render ability, I used XWalkView replace the WebView.
public boolean onKeyDown(int keyCode, KeyEvent event) {
WebView mWebView = (WebView) findViewById(R.id.webview);
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if (mWebView.canGoBack()) {
mWebView.goBack();
} else {
finish();
if (MainActivity.mTencent.isSessionValid()) {
MainActivity.logout();
}
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
When change to XWalkView, I only find this about go back in the XWalkView. But I cannot find an example about to use it. When I not implement the back button envent, the app will exit after I double click the back button.
My question is: 1. How to use go back in the XWalkView, if some code may be more helpful. 2. How can I disable the back button click event, when I not use the go back functon.
Thank you in advance.
回答1:
After days of digging, I solved this: put this in the activity xwalkview in. Though this works, but the go back sometimes lost some history. So I also want someone give a better answer here.
for goback:
public boolean onKeyDown(int keyCode, KeyEvent event) {
//WebView mWebView = (WebView) findViewById(R.id.webview);
XWalkView mXWalkView = (XWalkView) findViewById(R.id.xWalkView);
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if (mXWalkView.getNavigationHistory().canGoBack()) {
mXWalkView.getNavigationHistory().navigate(XWalkNavigationHistory.Direction.BACKWARD, 1) ;
} else {
finish();
if (MainActivity.mTencent.isSessionValid()) {
MainActivity.logout();
}
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
for disable back event you can override either of these method dispatchKeyEvent
, onBackPressed
, onKeyDown
. Refer to this answer for more.
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
// TODO Auto-generated method stub
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
return true;
}
return super.dispatchKeyEvent(event);
}
回答2:
You can use xwalkviewhistory.cangoback()
来源:https://stackoverflow.com/questions/40968633/how-to-use-back-button-to-go-back-with-xwalkview-of-crosswalk-or-disable-it