Pressing back button exits the app instead of navigating backwards in the WebView
Add this in your Activity
code (not in onCreate()
or nested anywhere else)
@Override
public void onBackPressed() {
if (web.copyBackForwardList().getCurrentIndex() > 0) {
web.goBack();
}
else {
// Your exit alert code, or alternatively line below to finish
super.onBackPressed(); // finishes activity
}
}
This will navigate back through the WebView
history stack until it is empty and then perform the default action (in this case it finishes the activity).
You should probably also set your WebViewClient to have shouldOverrideUrlLoading
return true
or you won't load any links.