I have a WebViewClient in an Activity in which I override the onKeyDown method like this
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
webView.goBack();
return true;
} else {
finish();
return true;
}
}
This works like a charm on my phone as well as the emulators I tested on including a 3.0 emulator.
Weird thing is that on a 3.1 emulator as well as on my Xoom tablet (3.0.1) it does NOT work. It seems that webView.canGoBack() always returns true on these platforms.
Questions:
Has anybody else found similar behaviour?
Do you have a workaround/hack that allows me to make the backbutton work to navig开发者_C百科ate in the web view history as well as ultimately out of the activity if required?
Update: I have since then change the app to use fragments with the compatbility library so I am now using this:
webView.setOnKeyListener(
new View.OnKeyListener() {
@Override
public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
webView.goBack();
return true;
} else {
redirectHelper.finish();
return true;
}
}
}
);
where redirect helper basically is a wrapper for proper finishing of an activity or removing a fragment from the stack. Still has the same issue though..
I'm using this without issues on 3.1 and Galaxy Tab 10.1. Haven't tried onKeyDown method.
@Override
public void onBackPressed() {
if( webView.canGoBack() ) {
webView.goBack();
} else {
super.onBackPressed();
}
}
精彩评论