I have an Activity in my app which has a Webview in it which opens a URL provided to it and is then redirected to the landing page of the website. The issue I'm facing is once the user is on landing page the redirection is same page redirection i.e. if the landing page is
www.example.com/landing
The redirection is
www.example.com/landing/#other_page
Which does not call the
onPageStarted(WebView view, String url, Bitmap favicon)
of the webview, but
onPageFinished(WebView view, String url)
is called.
Is there a way to track when the redirect is starting to load since I need to log the time required to load a page even if the page is same page redirection.
Thanks in advance!
Edit: According to the documentation:
onPageStarted will not be called when the contents of an embedded frame changes, i.e. clicking a link whose target is an iframe, it will also not be called for fragment navigations (navigations to #fragment_id).
Which is exactly my case. Is there any other way to track the start and end of loading of fragment navigation?
I think you are looking for that method
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.d("Webview", url);
return false;
}
});
Related
My Android app uses a WebView to let the user navigate around the Internet, but occasionally it encounters a site that navigates using its own funky schema. For example, if you search for "aliexpress", the app will take you to the site's Home page just fine.
However, if you select any of the buttons on the top, WebView throws an error:
All I needed to do was call shouldOverrideUrlLoading() as follows:
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
Uri current_page = request.getUrl();
if (!current_page.toString().startsWith(("http://")) && !current_page.toString().startsWith(("https://"))) return true;
return false;
}
I have an application with WebView. In my WebView, I will get next page URL from:
public boolean shouldOverrideUrlLoading(WebView view, String URL)
and I'm saving this URL for further process. I'm also using:
webView.loadUrl(baseUrl)
By using shouldOverrideUrlLoading function, I will get the URLs loading by my WebView. But in facebook case (page loaded as mobile view), I didn't get the URLs which I'm switching through facebook.
For instance: First I loaded facebook I got that URL, then I go to my home, then my page, then events, then other people profile, pages etc. I didn't get any of the URLs except the first one.
In an Android web view, I'm trying to get the URL of webpages visited by the user. I'm using:
public void onPageStarted(WebView view, String url, Bitmap favicon)
public boolean shouldOverrideUrlLoading(WebView view, String url)
public void onPageFinished(WebView view, String url)
Unfortunately, for certain cases all of the above return me the wrong URL. For example when I'm on http://www.yahoo.com I get the correct URL, but once I click on any news article, the URL returned to me is still http://www.yahoo.com.
Just to confirm, I opened http://www.yahoo.com on Chrome, and then tapped the same articles to check whether Chrome gets the correct URLs. It does, which means it's possible and I'm probably missing something fundamental.
How can I get that URL in my own webview? Note that I have tried the JavaScript interface as well, but it's been of no use.
When is shouldOverrideUrlLoading method called?
webView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
}
});
Is it called during initial loading of url? e.g. webView.loadUrl( "file:///android_asset/html/index.html");
Is it called everytime URL of webview changes?
Any reference? I didn't find one. Thanks
It does however, get called when the WebView to load a different URL from the one the user had requested.
Calling loadUrl() will also trigger the shouldOverrideUrlLoading() method. (Only when a new url is about to be loaded.)
Give the host application a chance to take over the control when a new url is about to be loaded in the current WebView. If
WebViewClient is not provided, by default WebView will ask Activity
Manager to choose the proper handler for the url. If WebViewClient is
provided, return true means the host application handles the url,
while return false means the current WebView handles the url.
Ref : public boolean shouldOverrideUrlLoading (WebView view, String url)
Below is the answer for your both the questions:
As per the document, it will manage every time new URL is about to load in current WebView.
The app that I am making uses dropbox, users must give authorisation to enable us to do this. To do this I load a url in a webview which opens up the login for dropbox then shows a "connect" button.
The first problem I had was the webview was opening the default browser. So I could understand why it was doing this I added a WebViewClient and Overrided the onPageStarted method like so..
class AuthCodeWebViewClient extends WebViewClient {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
Log.d(TAG, url);
}
}
Doing this stopped the default browser opening but I cannot press the button within the html. The webview is recognising it as I recieve this in the log...
07-17 16:13:16.700: V/webview(29778): singleCursorHandlerTouchEvent
-getEditableSupport FASLE 07-17 16:13:17.025: I/GATE(29778): DEV_ACTION_COMPLETED
What am I doing wrong so the button doesn't work?
It turns out it was pretty simple and I figured it out about a minute after I posted. I needed to enable javascript
webview.getSettings().setJavaScriptEnabled(true);