You have onBackPressed()
method to override instead of onKeyDown()
. Replace onKeyDown()
with onBackPressed()
as below:
@Override
public void onBackPressed() {
if (myWebView.canGoBack())
myWebView.goBack();
else
super.onBackPressed();
}
EDIT
Also check 3rd line in your onCreate()
WebView myWebView = (WebView) findViewById(R.id.webview);
You are declaring myWebView
as local variable again and global variable is not instantiated which is throwing NullPointerException
and crashes the app.
So change the your 3rd line of code as below:
myWebView = (WebView) findViewById(R.id.webview);