redfist / google-mobile-dev

Automatically exported from code.google.com/p/google-mobile-dev
0 stars 0 forks source link

GoogleAdView's WebView is never destroyed #9

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
http://groups.google.com/group/afma-developers/browse_thread/thread/7c119090cc7a
5523

android.webkit.WebView member of GoogleAdView is never destroyed. DDMS shows 
the WebViewCoreThread continuing to run in the background (4 utime/s) which is 
causing performance issues and contributing to a sharp decrease in battery life.

Original issue reported on code.google.com by canoe%go...@gtempaccount.com on 22 Jul 2010 at 11:31

GoogleCodeExporter commented 8 years ago
This Is very bad form... Please fix 

Original comment by chomp...@gmail.com on 4 Aug 2010 at 11:14

GoogleCodeExporter commented 8 years ago
It's possible to pause the WebView's JS execution when the activity is paused; 
this will send WebViewCoreThread to the WAIT state, where it will not consume 
resources.  Before you do so, it's important to note that WebView.pauseTimers() 
and WebView.resumeTimers() are reference counted, so you'll want to have one in 
onPause() and one in onResume(), respectively.

Here's some sample code I added to my activity:
@Override
public void onPause() {
    GoogleAdView adView = (GoogleAdView) findViewById(R.id.adview);
    pauseWebView((ViewGroup)adView);
    super.onPause();
}

public void pauseWebView(ViewGroup v) {
    for(int i=0; i<v.getChildCount(); i++) {
        View child = v.getChildAt(i);
        if(child instanceof WebView) {
            ((WebView) child).pauseTimers();
            Log.i("JS", "Found webview, pausing");
        } else if(child instanceof ViewGroup) {
            pauseWebView((ViewGroup)child);
        }
    }
}

@Override
public void onResume() {
    GoogleAdView adView = (GoogleAdView) findViewById(R.id.adview);
    resumeWebView((ViewGroup)adView);
    super.onResume();
}

public void resumeWebView(ViewGroup v) {
    for(int i=0; i<v.getChildCount(); i++) {
        View child = v.getChildAt(i);
        if(child instanceof WebView) {
            ((WebView) child).resumeTimers();
            Log.i("JS", "Found webview, resuming");
        } else if(child instanceof ViewGroup) {
            resumeWebView((ViewGroup)child);
        }
    }
}

Original comment by wesgood...@google.com on 7 Sep 2010 at 6:50

GoogleCodeExporter commented 8 years ago
If this is the long term fix could you add the resume and pause methods to the 
Adview API in a future release?

Original comment by paul.rashidi@gmail.com on 14 Oct 2010 at 10:08

GoogleCodeExporter commented 8 years ago
Many users compained that my App is "always running". 

Original comment by damjan.m...@gmail.com on 3 Mar 2011 at 6:43

GoogleCodeExporter commented 8 years ago
Thank you in this thread for mentioning that JWebCoreJavaBridge.pause() and 
resume() were reference counted. I wasn't looking in there until I saw this 
thread. Now I see why my WebView is not pausing.

JWebCoreJavaBridge.mPauseTimerRefCount is initialized to 0.

In JWebCoreJavaBridge.java:

public void pause() {
    if (--mPauseTimerRefCount == 0) {
        mTimerPaused = true;
        mHasDeferredTimers = false;
    }
}

So the function does nothing unless the mPauseTimerRefCount is 1.

If you create a new WebView you have to call resumeTimers() on it from the 
start so that mPauseTimerRefCount can be set proper to the other 
JWebCoreJavaBridge state (mTimerPaused etc).

Otherwise if you do not initially set it, you cannot fully pause.

Original comment by cfaher...@gmail.com on 9 Mar 2011 at 6:44

GoogleCodeExporter commented 8 years ago
The AFMA SDK has been superseded by the Google AdMob Ads SDK.

Original comment by wesgood...@google.com on 25 Jul 2011 at 10:37

GoogleCodeExporter commented 8 years ago
this issue happen again after upgraded to 4.3 :(

Original comment by superbiji on 13 Aug 2013 at 3:56

GoogleCodeExporter commented 8 years ago
Funny how 5 years later this appears to still be a problem! My apps using ads 
are consuming CPU even in background due to this!

Original comment by androtu...@gmail.com on 18 Aug 2015 at 3:39

GoogleCodeExporter commented 8 years ago
Yep, I was just thinking about the same thing the other day doing some 
profiling on an old implementation I had to go back to - for how long Admob / 
Google Play Services developers have stuck to this WebView implementation to 
leverage its complex functionality, instead of maybe exploring a simpler, 
standalone implementation to avoid exactly these kind of problems. I guess one 
hope is that hardware will catch up eventually with expectations and there is 
no need to rework anything.

New AdView.onPause() doesn't seem to do that much:
https://developers.google.com/android/reference/com/google/android/gms/ads/AdVie
w

Have a read on this thread as well when calling WebView.pauseTimers() cause it 
will affect all other WebViews in your application, not just the ad WebViews.
https://groups.google.com/forum/#!topic/google-admob-ads-sdk/Qu4G19NFAuI

Original comment by dan.dra...@gmail.com on 18 Aug 2015 at 5:49