I have a local website that is Javascript controlled and am loading it in a WebView. The website is really one main page with an iframe whose content changes based on user input. There is a "Next" button on the main page that runs some javascript functions and decides which page to load in the iframe (via document.getElement(...).src = )
In Android 2.1 things were working fine - simulated and on phone. The webview was loading all the pages properly. I tested it on a 2.2 phone and whenever I click Next the page (that should be loaded in the iframe) is loaded in the default browser.
I defined an intent filter
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:scheme="file"
android:host="*"
android:pathPrefix="sdcard/Android/data/android.website/files/pages"
/>
</intent-filter>
hoping that it would redirect the page requests to the activity, but it doesn't work. The url is still loaded by the browser.
Using shouldOverredeUrlLoading doesn't work well because the link that is loaded is the one of the internal frame. I tried loading instead javascript code that sets that url as the src of the iframe, but didn't work.
It's weird that this only happens on the 2.2 phone. In a 2.2 emulator it works fine.
Any ideas?
Just confirming that overriding the shouldOverideUrlLoading method (as described above) worked for me as well...I happen to be using phonegap, so i needed to override phonegap's GapViewClient object as shown below. Same basic concept though.
I believe shouldOverrideUrlLoading only gets called for the initial page URL -- not iFrames. Check out onPageFinished (also in the WebViewClient). That generally gets called for each iFrame, although I have seen differences in behavior between Android 2.1 and 2.2.
Note that onPageFinished returns void, so there is no way to tell the WebView not to draw the frame -- and in any event it should have drawn it by that point. But you could always monitor URLs in onPageFinished, then when you see the URL of interest, hide the WebView (where you may need to switch to another view) and then start your new activity.
Android 4.x seems to behave as follows:
shouldOverrideUrlLoading
is called only when the whole page is about to change.The iframe is only one part of the page. Therefore
shouldOverrideUrlLoading
is not called.Overriding
public WebResourceResponse shouldInterceptRequest(WebView view, String url)
worked for me.There seems to be no proper way to stop the
WebResourceResponse
. You can load another URL instead though:Try providing your own
WebViewClient
to WebView. You should overrideshouldOverrideUrlLoading(..)
and returnfalse
.