How to use back button to go back with XWalkView of CrossWalk, or disable it?

蹲街弑〆低调 提交于 2019-12-10 11:06:32

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!