I use something like this in my activities with WebViews:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if (mWebView.canGoBack()) {
mWebView.goBack();
} else {
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
Edit:
For this code to work, you need to add a field to the Activity
containing the WebView:
private WebView mWebView;
Initialize it in the onCreate()
method and you should be good to go.
mWebView = (WebView) findViewById(R.id.webView);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…