Calling the "shouldOverrideUrlLoading" method on webview.goBack ()

I load all of my pages inside the shouldOverrideUrlLoading method on a webview (mainly to track the current URL of the page and make some changes).

My problem is that the back button (ex: webview.goback() ) bypasses the function above.

Is there a way that I can explicitly call both goback() and shouldOverrideUrlLoading() methods.

 public boolean shouldOverrideUrlLoading(WebView view, String url){ //get current url and do some modifications to html} public boolean onKeyDown(int keyCode, KeyEvent event) { if ((keyCode == KeyEvent.KEYCODE_BACK) && web1.canGoBack()) { webview.goBack(); return true; } return super.onKeyDown(keyCode, event); } 
+4
source share
1 answer

If you look at your code as soon as the shouldOverrideUrlLoading method shouldOverrideUrlLoading called based on some logic for a specific URL action, you need the Webview method back. I suggest you use the following method inside an action:

 @Override public void onBackPressed() { if ((keyCode == KeyEvent.KEYCODE_BACK) && web1.canGoBack()) { webview.goBack(); } else { super.onBackPressed(); } } 

which does the same as onKeypressed. (It uses onBackPressed instead of onKeypressed , but write this code inside the Activity class, but not inside the custom WebViewClient .

Call onBackPressed() inside shouldOverrideUrlLoading . Try this should work.

+1
source

All Articles