6

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.

IVlad
  • 43,099
  • 13
  • 111
  • 179
  • 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? – pgsandstrom Aug 29 '12 at 07:40
  • @pgsandstrom - no, I have no idea what it uses. I used the term `activity` informally. I am open to any solution - I'm not very familiar with fragments so I'll have to look into that. – IVlad Aug 29 '12 at 07:43
  • @pgsandstrom - it seems framents are only available in API level >= 11. I need this to work at least on Android 2.3, if not earlier versions too. I will edit my question. – IVlad Aug 29 '12 at 07:46
  • You can use the android compability package to get support from api level 4: http://developer.android.com/tools/extras/support-library.html – pgsandstrom Aug 29 '12 at 08:16
  • @pgsandstrom - oh, right. Will I have to implement my own handling of events like pressing the back button, to make it cycle between the "fragments stack" and not the activity stack, or is this handles automatically? By the way, you should post this as an answer. – IVlad Aug 29 '12 at 16:31
  • You will have to handle it 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 :) – pgsandstrom Aug 29 '12 at 16:48

1 Answers1

1

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 :)

IVlad
  • 43,099
  • 13
  • 111
  • 179
pgsandstrom
  • 14,361
  • 13
  • 70
  • 104
  • Bad grammer, bad answer style -- does not give a reason as to why. This answer is of little use to someone who encounters a similar issue and comes looking for an answer. – Joseph Earl Aug 29 '12 at 16:50
  • @JosephEarl - he answered in comments, I fixed it for him now. I'll accept this in a day or two if no one suggests something better. – IVlad Aug 29 '12 at 16:54