I am facing an issue in WebView in devices running API 27 (8.1.0).
I pass off my params in the userAgentString based on which a php page loads the appropriate data. When I open the WebView for the first time it works well.
However, whenever I open the WebView after this first time (I call setUserAgentString each time I load it, with fresh values), it persists with that old value of userAgentString, thus not loading the correct data. This happens irrespective of whether I am in the same activity and re-using the same WebView, or move to a different activity where a new instance of WebView is used.
The following devices gave this odd behaviour -
Google Pixel 2XL, version 8.1.0
Xiaomi Mi A1, version 8.1.0
...and the webview was working perfectly on the following devices, not showing this behaviour -
Samsumg GALAXY Tab SM-T825, version 8.0.0
Moto G4, version 7.0.0
HTC Desire 526, version 4.4.2
I think this could be linked with how the Android Webview implementation has been changed to Chrome in Oreo.
However, the behaviour did not change irrespective of what I set as "Webview Implementation" in Developer settings, implying that may not be the cause.
Additionally, only when I clear data, or do a fresh install, is the userAgentString value reset.
I have read dozens of posts on SO, and tried all sorts of methods related to WebView and WebSettings, but I haven't managed to find a solution.
What am I missing here?
Maybe, this code can help you.
webView.clearCache(true);
webView.clearHistory();
webView.getSettings().setAppCacheEnabled(false);
webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
Lucky :)
I experienced this issue recently and the problem was that the device had an outdated Chrome WebView framework. Simply update it via the Play Store and the issue should resolve.
Related
Since there is a known bug in Android Kitkat versions (most commonly 4.4.2) that disallows users to open file upload dialog after tapping any HTML file element within WebView, can Android itself determine if current device suffers from this bug?
Determining only by Android version is not a good solution since not all devices running 4.4.2 suffer from this bug.
I had to deal with this problem too. Eventually I decided it's not worth it because not that many device modifies the stock webkit, so I applied the workaround based on version number alone (SDK_INT == 19)
However if webkit is really the culprit (and not some other system class), you may want to detect the webkit version: https://stackoverflow.com/a/29218966/828752
From https://developer.chrome.com/multidevice/webview/overview#what_version_of_chrome_is_it_based_on_:
The WebView shipped with Android 4.4 (KitKat) is based on the same
code as Chrome for Android version 30. This WebView does not have full
feature parity with Chrome for Android and is given the version number
30.0.0.0. The updated WebView shipped with Android 4.4.3 has the version number 33.0.0.0.
This detection method isnt very elegant however as there is no precise way to detect occurrence of this bug on a device best way is to count via javascript how many times a user has clicked the HTML FILE element.
If the counter of clicks exceeds some value (because the user is trying to get it work over and over) you know its not working. At this point by far most common version with this bug is 4.4.2 so you can activate this counter only when this Android version is detected for extra layer of precision.
Like I said, this detection method is quite messy however if Android has a bug this is as good as it gets. Users who dont experience this bug are unlikely to click more than once the HTML FILE element.
I'm currently using WebView.draw() to take a screenshot of the entire WebView and save it into a Bitmap (based off of this solution). It has worked perfectly up until now.
I was recently testing this functionality on a Nexus 5 (please note that on the Nexus 5 the WebView in question is bigger than the screen, so the user can scroll both horizontally and vertically) running Lollipop 5.1 and the latest update of Android System WebView (version 42.0.2311.137 at time of writing) when I noticed that the generated bitmap was wrong. It had the correct dimensions but instead of showing the whole WebView, it only had the part of the WebView that was visible to the user when the method was called.
I have tried uninstalling all updates to the Android System WebView (thus bringing it back to version 39 (1743759-arm) and with the factory version the functionality works fine. So this problem is due to a relatively recent update of the Android System WebView. I wasn't able to find a change log so I can't figure out what has changed and how to fix it.
Below is a quick visual explanation:
Has anyone else encountered this problem and found a possible solution ?
You need to call WebView.enableSlowWholeDocumentDraw() before creating any WebViews. That is, if you have any WebViews in your layout, make sure you call this method before calling setContentView() in your onCreate().
I've experienced a LOT of strange behaviours with using WebView in Android and I'm wondering why there are so many diffrences between the WebView and the Browserinstalled on each phone?
As an example, I've developed some applications that had to display HTML content, which contained either jquery-mobile, flash,javascript, YouTube embedded and so on. So I had a lot of problems with displaying this pages inside WebViews. Either they wouldn't get displayed at all, just blank, either the videos won't play and so on. And the strange thing is that they work properly if opened in the Browser installed on the phone. I had JavaScript enabled, I tried diffrent WebSettings, I had set the WebChromeClient and WebViewClient looking for javascript errors... but nothing worked.
So I got to the conclusion that the WebView component is completely different from the Browser application installed on the phones. I'm thinking that every manufacturer makes their own Browser to support as many as possible pages, and the WebView remains the standard one, included in the Android SDK.
Am I right? Or there is another reasons/explanation for this?
Thanks.
EDIT: Everything that #ondoteam has suggested was enabled and set at the time being. I no longer have the references to that websites, which anyway were internal.
This article outlines your speculation about stock browser differences between manufacturers, that absolutely is true: 5 reality checks every team needs before working on Android webkit
...which does cause trouble and mysterious/difficult to diagnose/solve problems.
As far as your issues with your WebView implementation:
Version of jquery-mobile may be an issue jquery-mobile loaded into an android WebView if you are using jquery.mobile-1.0.1.min.js you may want to try the uncompressed jquery.mobile-1.0.1.js
And Flash :-/ Good luck:
Screen blinking when using a webview with flash
Flash in WebView not working on Android 3.2
Flash video not displaying in WebView for Android 3.0.1
For loading SWF:
Load an SWF into a WebView
loading flash files (.swf) webview in android
Good luck with that, seems like a lot of variables with devices, Android versions, etc. And will take persistence with trial/error.
The stock browser and WebView differs .Using all the tweaks like
WebView browser;
browser.clearFormData();
browser.clearHistory();
browser.clearCache(true);
browser.getSettings().setAppCacheEnabled(true);
browser.getSettings().setDatabaseEnabled(true);
browser.getSettings().setDomStorageEnabled(true);
browser.setWebChromeClient(new WebChromeClient());
still it does not load high resolution images properly which is being loaded perfectly well by the android browser.
Only part of the image can be visible on the webview screen which appears fully on normal android browser.
This behavior is observed with latest Android 4X SDK as well.
Which means android default browser significantly tweaks the webkit/webview code to show any url
WebView by default is restricted. You should call setWebChromeClient and family to have a decent browsing experience. In addition, don't forget setJavaScriptEnabled() and other similar stuff. Sorry for my brevity... I think that you don't need examples.
http://developer.android.com/reference/android/webkit/WebView.html
have you checked this question : jquery-mobile loaded into an android WebView
it might be that you're using the wrong libraries. Just check in any case.
The URL which points to the user "coopthink" which works in most browsers, including Chrome and Safari on iOS is:
http://www.twitter.com/coopthink
However, if I try this on Android (I'm writing an app, but this even occurs directly in the browser), it redirects to:
https://mobile.twitter.com/#!/coopthink
and then says "Sorry, that page doesn't exist". Any idea what is wrong and what would be the correct URL?
EDIT: This seems to work on Android 2.2 and higher; only 2.1 (and possibly lower) show this problem.
Try this on a 2.2 and 2.1 emulator. I was getting some strange behavior previously with Android 2.2 and meta-tags on an emulator. See my previous post. It seems to be a bug with hashes and lower versions of the emulator code. I haven't tested it though on an actual device of the same generation.
This is an obscure, possibly hardware dependent error.
I have a webview that is trying to display a twitter feed like this: http://twitter.com/HP
The view works fine on a Droid 2, but fails on Droid and Droid X.
Instead of displaying the page, it just displays the twitter logo and hangs forever.
about the devices:
All running the same software, and Android 2.2. However, they are running different flavors of Android 2.2.
Droid X: 2.2.1
Droid 1: 2.2.2
Droid 2: 2.2
It seems that Verizon must have added a bug or fixed a bug in their later versions of 2.2.
This is the answer:
add this:
webView.getSettings().setDomStorageEnabled(true);
(plus use code from here:
http://www.codelark.com/2010/09/13/backwards-compatibility-in-android-using-reflection/)
I have no idea why, but if you ever see an error message like this:
Web Console: Uncaught TypeError: Cannot read property 'profile' of undefined
then try adding this setting.