views:

253

answers:

2

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?

A: 

I'm seeing the problem you are having. It looks like several threads start and remain running in the background:

ID   Tid      Status      utime  stime  Name
--   ---      ------      -----  -----  ----
8    27517    wait        107    7      WebViewCoreThread   
9    27519    timed-wait  0      0      CookieSyncManager   
10   27520    wait        1      0      http0   
11   27521    wait        1      0      http1   
12   27522    wait        0      1      http2   
13   27523    wait        0      0      http3   
14   27524    wait        10     6      WebViewWorkerThread 
16   27530    timed-wait  1      1      pool-1-thread-1 
17   27527    timed-wait  0      0      IdleReaper  
18   27528    native      0      0      Binder Thread #3    
19   27529    native      0      0      Binder Thread #4    
20   27531    timed-wait  0      1      pool-1-thread-2 

By the looks of it, they get re-used when you come back around so I guess no harm no foul?

I too would like to be able to stop and destroy these threads as it would free up some memory for other activities that the user might be headed to.

Beshoy Girgis
A: 

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

Skymt
Thanks for you answer. As the bug report in the url you provided says, it might work, but it "feels a little dirty".I guess for now it is the best answer to this question.
Amit