Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebView threads never stop (WebViewCoreThread, CookieSyncManager, http[0-3])

I use a WebView to display some internet content on one of our app's Activities.
The problem is that when the user switches out of this activity, WebView's threads keep running!
The problematic threads are:

Thread [<17> WebViewCoreThread] (Running) Thread [<25> CookieSyncManager] (Running) Thread [<19> http0] (Running) Thread [<29> http1] (Running) Thread [<31> http2] (Running) Thread [<33> http3] (Running) 

Pausing each one of these threads, and checking what it is busy doing:

Thread [<17> WebViewCoreThread] (Suspended)     Object.wait(long, int) line: not available [native method]     MessageQueue(Object).wait() line: 288     MessageQueue.next() line: 148     Looper.loop() line: 110     WebViewCore$WebCoreThread.run() line: 471     Thread.run() line: 1060  Thread [<25> CookieSyncManager] (Suspended)     Object.wait(long, int) line: not available [native method]     MessageQueue(Object).wait(long) line: 326     MessageQueue.next() line: 144     Looper.loop() line: 110     CookieSyncManager(WebSyncManager).run() line: 90     Thread.run() line: 1060  Thread [<19> http0] (Suspended)     Object.wait(long, int) line: not available [native method]     RequestQueue(Object).wait() line: 288     ConnectionThread.run() line: 93 

I wonder how can I tell the Looper in each of those threads to quit.

I tried calling webView.destroy() in the activity's onPause() method, but it had no influence.
When I disable the call for opening a web page in the webView ( webView.loadUrl(...) ), those threads naturally are not started, and therefore don't stay on after leaving the activity.

Any ideas as to how I can make WebView's threads stop after leaving their activity?

like image 551
Amit Kotlovski Avatar asked Jan 11 '10 09:01

Amit Kotlovski


People also ask

How do I stop Youtube playing on Android WebView?

setBuiltInZoomControls(false); webView.

How do you override a WebView?

If you want to override certain methods, you have to create a custom WebView class which extends WebView . Also, when you are inflating the WebView , make sure you are casting it to the correct type which is CustomWebView . CustomWebView webView = (CustomWebView) findViewById(R. id.

Is Android WebView deprecated?

Beginning October 5, 2021, Facebook Login will no longer support using Android embedded browsers (WebViews) for logging in users.


2 Answers

You should be able to stop / resume these threads by calling the onPause / onResume on the webview.

Those are however hidden, so you will need to do it through reflection. The following code worked for me:

Class.forName("android.webkit.WebView").getMethod("onPause", (Class[]) null).invoke(webView, (Object[]) null); 

Where webView is the instance of WebView.

Also see: http://code.google.com/p/android/issues/detail?id=10282

like image 197
Skymt Avatar answered Oct 11 '22 12:10

Skymt


My solution, in extended webview class(tested in android 2.2, andriod 2.3, android 3.1):

 private boolean is_gone = false;  public void onWindowVisibilityChanged(int visibility) {      super.onWindowVisibilityChanged(visibility);      if (visibility == View.GONE) {          try {              WebView.class.getMethod("onPause").invoke(this); //stop flash          } catch (Exception e) {}          this.pauseTimers();          this.is_gone = true;      } else if (visibility == View.VISIBLE) {          try {              WebView.class.getMethod("onResume").invoke(this); //resume flash          } catch (Exception e) {}          this.resumeTimers();          this.is_gone = false;      }  }  public void onDetachedFromWindow() { //this will be trigger when back key pressed, not when home key pressed      if (this.is_gone) {          try {              this.destroy();          } catch (Exception e) {}      }  } 
like image 45
diyism Avatar answered Oct 11 '22 14:10

diyism