I am trying to figure out a way to handle the WebView back stack similar to how the Android web browser handles it when the back button is pressed from within my own app\'s WebV
boolean isGoBack = false; //back key is pressed
private long goBackTime = 0; //
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
isGoBack = true;
goBackTime = System.currentTimeMillis();
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
Webview mWebView.
setWebViewClient(new WebViewClient() { //setWebViewClient
@Override
public boolean shouldOverrideUrlLoading (WebView view, String url){
if (!TextUtils.isEmpty(startUrl) && !startUrl.equals(url) && isGoBack && System.currentTimeMillis() - goBackTime < 600) {
isGoBack = false;
if (mWebView.canGoBack()) {
mWebView.goBack();
} else {
finish(); //activity finish
}
} else {
view.loadUrl(url);
isGoBack = false;
return true;
}
return false;
}
@Override
public void onPageStarted (WebView view, String url, Bitmap favicon){
super.onPageStarted(view, url, favicon);
startUrl = url;
}
}