WebView load on android 5.0 sometimes cause app stuck and crash - android

I use WebView to load urls and also send and receive Java script calls.
webView.setWebChromeClient(new WebChromeClient() {
#Override
public void onProgressChanged(WebView view, int newProgress) {
if (newProgress == 100) {
showContentOrLoadingIndicator(true);
}
}
});
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
The code works fine on android < 5 but on lollipop versions sometimes the app simply get totally stuck and sometimes crash.
Any idea why?

Related

Android Webview Not Working After Updating Android System webview

Making calls to webpage via javascript interface stopped working after updating Android System webview to v- 92.0.4515.105. This issue is mostly on Samsung devices, running Android 11. Works perfectly fine on other devices.
This is my settings:
// attach the interface
webView.addJavascriptInterface(this, "Android");
//setup webview settings
private void setupWebViewClients() {
this.webView.getSettings().setJavaScriptEnabled(true);
this.webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
this.webView.getSettings().setDomStorageEnabled(true);
if (Build.VERSION.SDK_INT >= 19) {
this.webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
this.webView.getSettings().setAppCacheEnabled(true);
}
else {
this.webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
this.webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
//App logic
});
this.webView.setWebViewClient(new WebViewClient() {
public void onPageStarted(WebView view, String url, Bitmap favicon) {
}
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//App logic
}
public void onPageFinished(WebView view, String url) {
//App logic
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
}
});
}
And I am calling functions this way:
this.webView.loadUrl("javascript:updateGps('dumny',dummy')";
Is there any other settings introduced in Android 11 that I am not aware of? Or it's the bug in Google's Android system webview? Please help me out if there's any work around. Thank you.

Android WebView can't always load the url

Im trying to load a URL using WebView, but the web view does not seem stable - sometimes the page loads 100%, sometimes it stops loading on 30%/50%/75% (random).
I have the WebView being created inside an Android Bolts task, and the WebView is not being showed for the user, it only loads some javascript for future requests.
Code:
private Task<Boolean> task_SetupJavascript(){
final Task<Boolean>.TaskCompletionSource myTask = Task.create();
WebView webview = new WebView(getActivity());
final WebSettings settings = webview.getSettings();
settings.setJavaScriptEnabled(true);
settings.setDomStorageEnabled(true);
webview.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
view.loadUrl("javascript: LocalStorage.set('valueA', 'valueB', 'valueC');");
myTask.setResult(Boolean.TRUE);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
// never fired during tests
Log.e(TAG,"onReceivedError ---> Something went wrong... ");
myTask.setCancelled();
}
});
webview.setWebChromeClient(new WebChromeClient() {
#Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
Log.e(TAG, ""+newProgress);
}
});
webview.loadUrl(URL);
webview.onResume();
return myTask.getTask();
}
I can't figure out what can be causing the WebView to stop loading randomly in the middle of the progress.
Sometimes it fails the first time it runs this task, sometimes it fails after the 3/4/5 time.

Android web-view not working properly on device back press

Hi I am developing android in which I am using web-view. My web-view has more than one pages inside that. So my intended functionality is like that when I press device back button it should return to previous web url and when it on last web page it should close that activity and come to previous activity. I tried it in following way which works fine on new devices but not working properly on older android versions.My code looks like:
webView = new WebView(this);
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.contains(URLManager.URL_DICOM_BACK_PRESSED_URL)) {
processBackPressed();
return true;
} else {
return super.shouldOverrideUrlLoading(view, url);
}
}
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
if (failingUrl.contains(URLManager.URL_DICOM_BACK_PRESSED_URL)) {
processBackPressed();
} else {
showError("Server unreachable");
}
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
runOnUiThread(new Thread() {
#Override
public void run() {
super.run();
if(!webviewLoaded) {
webviewLoaded = true;
content.removeAllViews();
content.setOrientation(LinearLayout.VERTICAL);
// LLM.addInParent(content, textView, -1001, -999, 0);
LLM.addInParent(content, webView, -1001, 0, 1);
}
}
});
}
});
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(url);
#Override
public void onBackPressed() {
if (webView != null && webView.canGoBack()) {
webView.goBack();
} else {
processBackPressed();
}
}
public void processBackPressed() {
super.onBackPressed();
finish();
}
Above code works fine one new android versions but not working on older versions.
What I observe shouldOverrideUrlLoading not works on older versions.
Am I doing anything wrong? Need some help. Thank you.

PopUp screen not opening In webview?

I am Currently working with webview in which Javascript popup is created when some button is clicked in webview. But it is not opening in my Webview.
My code is Here:
wvPayements = (WebView) view.findViewById(R.id.wvPaymentsPage);
wvPayements.getSettings().setLoadWithOverviewMode(true);
wvPayements.getSettings().setUseWideViewPort(true);
wvPayements.getSettings().setSupportZoom(true);
wvPayements.getSettings().setAllowFileAccess(true);
wvPayements.getSettings().setJavaScriptEnabled(true);
wvPayements.getSettings()
.setJavaScriptCanOpenWindowsAutomatically(true);
wvPayements.getSettings().setBuiltInZoomControls(true);
wvPayements.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, final String url) {
dialog.cancel();
}
});
wvPayements
.loadUrl("http://192.168.0.114/Parch_ws/testalert.html");
You have to use WebChromeClient for your purpose:
One should use a WebChromeClient to get these alerts and the
functionality can be overwritten using theonJsAlert() method. Hope
this helps!
WebView wv=new WebView(this);
wv.setWebChromeClient(new WebChromeClient() {
#Override
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
//Required functionality here
return super.onJsAlert(view, url, message, result);
}
});
Source: https://issuetracker.google.com/issues/36905249#comment11
This can also happen if you are executing ES6 on unsupported browsers.
You can always check supported versions here

Android - Jelly Bean - YouTube play video only once

I am using a WebView in order to play YouTube vidoes in my Android app.
It worked fine, but I seem to have a problem with Jelly Bean (and maybe later versions, I don't know yet).
The first time I play a video it works fine, but when I try to play it again or even another video, after closing the WebView and re-open it, I see the video loading but then enters into pause mode (as can be seen in the image attched).
When closing the WebView I'm using this code, which I think is the cause of the problem:
Class.forName("android.webkit.WebView").getMethod("onPause", (Class[]) null).invoke(webview, (Object[]) null);
Did someone encounter this problem?
Left image - the video is played normally.
Middle image - trying to load it again.
Right image - video is paused and never started.
This is the code I'm using:
private void loadUrl()
{
progress.setVisibility(View.VISIBLE);
webview.setWebChromeClient(new WebChromeClient());
if (Build.VERSION.SDK_INT < 8) {
webview.getSettings().setPluginsEnabled(true);
} else {
webview.getSettings().setPluginState(PluginState.ON);
}
WebSettings webSettings = webview.getSettings();
webSettings.setJavaScriptEnabled(true);
webview.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView wView, String url)
{
progress.setVisibility(View.VISIBLE);
webview.loadUrl(url);
isLoading = true;
return true;
}
#Override
public void onPageStarted (WebView view, String url, android.graphics.Bitmap favicon)
{
progress.setVisibility(View.VISIBLE);
}
#Override
public void onReceivedError (WebView view, int errorCode, String description, String failingUrl)
{
progress.setVisibility(View.GONE);
isLoading = false;
}
#Override
public void onPageFinished (WebView view, String url)
{
progress.setVisibility(View.GONE);
isLoading = false;
}
});
webview.loadUrl(url);
}
I solved the problem by clearing/destroying the webview:
if (webview != null)
{
if (isLoading)
{
webview.stopLoading();
}
webview.clearCache(true);
webview.clearView();
webview.freeMemory();
webview.destroy();
}

Categories

Resources