title: How to avoid re-loading of Admob ads upon configuration change and starting an Activity?
body: <p>Using the banners sample <a href="https://github.com/googleads/googleads-mobile-android-examples" rel="noreferrer"><strong>here</strong></a>, I noticed that if I change the orientation, the ad is gone, and then reloads itself (takes time), so there is a time that nothing is shown, each time I change orientation.</p>
  
  <p>I know it's possible to set configChanges for the activity via the manifest (and it actually works), but this is not a recommended thing to do in general. It has multiple flaws:</p>
  
  <ol>
  <li>In my case, for example, changing the orientation let the toolbar stay on the same height, which is wrong because it should be different on landscape than on portrait. </li>
  <li>Going to another Activity the Admob view will still get re-loaded, even though it was loaded before.</li>
  <li>When going from portrait to landscape, and vice versa, the ad doesn't get to fit well on the new orientation.</li>
  </ol>
  
  <p>What else can be done to avoid re-loading of ads, on each configuration change?</p>
  
  <p>I want Admob to load ads once (and the rest in the background, for new ones), and then show them, and avoid re-loading when I change orientation. Just show what's best for the current configuration (orientation for example), each time I reach it.</p>
  
  <p>Same goes for going to a new Activity that has an Admob ad. If it loaded before, no need to re-load.</p>
  
  <hr>
  
  <p>EDIT: I've put a bounty on this, because Google/Firebase support told me via email it's actually possible to cache the ads:</p>
  
  <blockquote>
    <p>The Ads SDK won't cache ads for you, however you can implement an
    'AdManager' style class, most likely a singleton or an instance
    attached to your application instance, which can manage caching for
    you.</p>
    
    <p>As you recognize it's an Android pattern to destroy and recreate
    activities on orientation, instance state is usually saved and
    restored via a bundle, but this is not applicable to views.</p>
    
    <p>If you wish to optimize the reload time of the banner view in this
    scenario, I would recommend making your ad request via an AdManager
    class, and the AdManager can then apply some logic to cache ads such
    as your banner view for a certain amount of time, and return the
    previous banner if it is still valid for the new request (which you
    would make on orientation change).</p>
  </blockquote>
  
  <p>And :</p>
  
  <blockquote>
    <p>This idea of caching ads is not implemented by the SDK, as it is
    difficult to determine the best caching logic to apply in all cases,
    thus it is left to the developer to implement caching that suits their
    use case, if desired.</p>
    
    <p>Possibly having a banner ad decoupled from the 'AdView' class would
    make for more appropriate cacheable objects, however this is not
    currently the case.</p>
    
    <p>Therefore as it is, yes we suggest that in order to prefetch / cache a
    banner ad, you need to store the 'AdView'.  Instead of using an
    AdManager however, another supported and indeed recommended approach
    would be to use a ViewModel, see
    <a href="https://developer.android.com/guide/topics/resources/runtime-changes#RetainingAnObject" rel="noreferrer">https://developer.android.com/guide/topics/resources/runtime-changes#RetainingAnObject</a>
    and
    <a href="https://developer.android.com/topic/libraries/architecture/viewmodel" rel="noreferrer">https://developer.android.com/topic/libraries/architecture/viewmodel</a>. 
    These are designed for exactly this kind of use case.</p>
  </blockquote>
  
  <p>If it is indeed possible, I'd like to know exactly how to do it. Please, whoever succeeds in doing it, show in code what can be done.</p>