Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Having test my app with 2 different implementations of AdMob I found that implementing it via java code and not XML is match lighter for the app.</p> <p><strong>Update No1:</strong></p> <p>You can also add custom listeners to destroy after some time and recreate in order to handle it even better. Serverside there is also a parameter telling the app ad how soon should ask for a new ad, I am not sure if it exist in all cases but it is there for DFP accounts.</p> <p>A nice suggested way to implement the ad is that:</p> <pre><code>new Handler(new Handler.Callback() { @Override public boolean handleMessage(Message msg) { if (!isBeingDestroyed) { final AdRequest adRequest = new AdRequest(); final AdView adView = (AdView) findViewById(R.id.ad); adView.loadAd(adRequest); } }).sendEmptyMessageDelayed(0, 1000); </code></pre> <p>also do not forget to call <code>adView.destroy()</code> onDestroy() activity or when you do not want it any more!</p> <p>The above way is mentioned <a href="http://1gravity.com/index.php?option=com_content&amp;view=article&amp;id=71:android-and-memory-leaks&amp;catid=37:android&amp;Itemid=59" rel="nofollow noreferrer">here</a> with many useful memory releases!</p> <hr> <p><strong>Update No2:</strong> (improvement at <em>Update No1</em>)</p> <p>An improvement to the suggested handler way. Using that way you avoid (I hope) the handler callbacks that may be stacked when intentionally an activity created/destroyed before the delayed message is send. This is more likely to happen if you decide increase <code>1000</code> milliseconds:</p> <p>Create a Field for the handler:</p> <pre><code>private adHandler; </code></pre> <p>At your <code>onCreate</code>:</p> <pre><code>adHandler = new Handler(new Handler.Callback() { @Override public boolean handleMessage(Message msg) { if (!isBeingDestroyed) { final AdRequest adRequest = new AdRequest(); final AdView adView = (AdView) findViewById(R.id.ad); adView.loadAd(adRequest); } return false; } }); adHandler.sendEmptyMessageDelayed(0, 1000); </code></pre> <p>At your <code>onDestroy</code> do not forget to "release" the handler:</p> <pre><code>adHandler.removeCallbacksAndMessages(null); </code></pre> <p>null removes any callbacks see <a href="http://developer.android.com/reference/android/os/Handler.html#removeCallbacksAndMessages%28java.lang.Object%29" rel="nofollow noreferrer">doc</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload