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?
setBuiltInZoomControls(false); 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.
Beginning October 5, 2021, Facebook Login will no longer support using Android embedded browsers (WebViews) for logging in users.
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
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) {} } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With