I have a webview in my android app and would like to detect when the url changes.
I want to use this to hide the info button in the top bar when the user is on the info.php page and show it again when he is not on the info.php page.
I googled but can't find any working code, can anybody help me?
For those load url by javascript. There is a way to detect the url change by JavascriptInterface. Here I use youtube for example. Use JavaScriptInteface has async issue, luckily its just a callback here so that would be less issue. Notice that @javascriptinterface annotation must be existed.
Try to use
onLoadResource
. It will be called at least 1 time even if you are using JS to change your url. But it may be called more than one time, so be careful.I had the same problem. So i've solved this problem by overriding
public void onPageStarted(WebView view, String url, Bitmap favicon)
method as follows:Also you can override
public void onPageFinished(WebView view, String url)
method to get a new URL at the end of page load process.This will help to detect redirect caused by javascript.