Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent AdView from recreating itself for each activity

I have the following in all of my activities:

 <com.google.ads.AdView android:id="@+id/adView"
                           android:layout_width="wrap_content"
                           android:layout_height="wrap_content"
                           ads:adUnitId="..."
                           ads:adSize="BANNER"
                           ads:testDevices="TEST_EMULATOR"
                           ads:loadAdOnCreate="true"
                           android:layout_alignParentBottom="true"
                           android:layout_centerHorizontal="true"/>

The problem with this is that it reloads the ad every time I switch between activities, so there is a short period of time when no ad is displayed. Some applications don't have this problem, for example Bubble Shoot: when switching between activities, the ad doesn't disappear at all.

How can I achieve this effect? I tried setting loadAdOnCreate="false" and using a global AdRequest, but this still seems to recreate the actual control, so there is still a (very short, but noticeable) period when there is no ad displayed.

Note: I need this to work for all API levels >= 7 ideally. If not possible, >= Android 2.3 / API level 9 is also acceptable. It must also be independent of device type and screen size.

like image 332
IVlad Avatar asked Aug 29 '12 06:08

IVlad


1 Answers

Are you sure bubble shoot uses activities? Maybe they are using fragments for the different screens, and keeping the ad in a single activity or fragment?

You can use the android compability package to get support from api level 4: http://developer.android.com/tools/extras/support-library.html

You will have to handle navigation manually, overriding onBackPressed() and use the FragmentManager. It is a bit of a learning curve, but fragments can be very nice to work with once you get the hang of it :)

like image 86
pgsandstrom Avatar answered Sep 20 '22 17:09

pgsandstrom