Skip to content

Commit

Permalink
Update to ensure destroy() is called on all native ads.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 308834352
  • Loading branch information
Samuel Stow authored and maddevrelgithubbot committed Apr 28, 2020
1 parent 120ee56 commit c9c358e
Show file tree
Hide file tree
Showing 10 changed files with 291 additions and 157 deletions.
Expand Up @@ -46,7 +46,8 @@ public class MainActivity extends AppCompatActivity {
private static final String AD_MANAGER_AD_UNIT_ID = "/6499/example/native";
private static final String SIMPLE_TEMPLATE_ID = "10104090";

private UnifiedNativeAd nativeAd;
private NativeCustomTemplateAd nativeCustomTemplateAd;
private UnifiedNativeAd unifiedNativeAd;
private Button refresh;
private CheckBox requestNativeAds;
private CheckBox requestCustomTemplateAds;
Expand Down Expand Up @@ -261,18 +262,24 @@ private void refreshAd(boolean requestUnifiedNativeAds,

refresh.setEnabled(false);

AdLoader.Builder builder = new AdLoader.Builder(this, AD_MANAGER_AD_UNIT_ID);
AdLoader.Builder builder = new AdLoader.Builder(this, AD_MANAGER_AD_UNIT_ID);

if (requestUnifiedNativeAds) {
builder.forUnifiedNativeAd(new UnifiedNativeAd.OnUnifiedNativeAdLoadedListener() {
@Override
public void onUnifiedNativeAdLoaded(UnifiedNativeAd unifiedNativeAd) {
// If this callback occurs after the activity is destroyed, you must call
// destroy and return or you may get a memory leak.
if (isDestroyed()) {
unifiedNativeAd.destroy();
return;
}
// You must call destroy on old ads when you are done with them,
// otherwise you will have a memory leak.
if (nativeAd != null) {
nativeAd.destroy();
if (unifiedNativeAd != null) {
unifiedNativeAd.destroy();
}
nativeAd = unifiedNativeAd;
MainActivity.this.unifiedNativeAd = unifiedNativeAd;
FrameLayout frameLayout =
findViewById(R.id.fl_adplaceholder);
UnifiedNativeAdView adView = (UnifiedNativeAdView) getLayoutInflater()
Expand All @@ -287,25 +294,37 @@ public void onUnifiedNativeAdLoaded(UnifiedNativeAd unifiedNativeAd) {

if (requestCustomTemplateAds) {
builder.forCustomTemplateAd(SIMPLE_TEMPLATE_ID,
new NativeCustomTemplateAd.OnCustomTemplateAdLoadedListener() {
@Override
public void onCustomTemplateAdLoaded(NativeCustomTemplateAd ad) {
FrameLayout frameLayout = findViewById(R.id.fl_adplaceholder);
View adView = getLayoutInflater()
.inflate(R.layout.ad_simple_custom_template, null);
populateSimpleTemplateAdView(ad, adView);
frameLayout.removeAllViews();
frameLayout.addView(adView);
new NativeCustomTemplateAd.OnCustomTemplateAdLoadedListener() {
@Override
public void onCustomTemplateAdLoaded(NativeCustomTemplateAd ad) {
// If this callback occurs after the activity is destroyed, you must call
// destroy and return or you may get a memory leak.
if (isDestroyed()) {
ad.destroy();
return;
}
},
new NativeCustomTemplateAd.OnCustomClickListener() {
@Override
public void onCustomClick(NativeCustomTemplateAd ad, String s) {
Toast.makeText(MainActivity.this,
"A custom click has occurred in the simple template",
Toast.LENGTH_SHORT).show();
// You must call destroy on old ads when you are done with them,
// otherwise you will have a memory leak.
if (nativeCustomTemplateAd != null) {
nativeCustomTemplateAd.destroy();
}
});
nativeCustomTemplateAd = ad;
FrameLayout frameLayout = findViewById(R.id.fl_adplaceholder);
View adView = getLayoutInflater()
.inflate(R.layout.ad_simple_custom_template, null);
populateSimpleTemplateAdView(ad, adView);
frameLayout.removeAllViews();
frameLayout.addView(adView);
}
},
new NativeCustomTemplateAd.OnCustomClickListener() {
@Override
public void onCustomClick(NativeCustomTemplateAd ad, String s) {
Toast.makeText(MainActivity.this,
"A custom click has occurred in the simple template",
Toast.LENGTH_SHORT).show();
}
});
}

VideoOptions videoOptions = new VideoOptions.Builder()
Expand Down Expand Up @@ -334,8 +353,11 @@ public void onAdFailedToLoad(int errorCode) {

@Override
protected void onDestroy() {
if (nativeAd != null) {
nativeAd.destroy();
if (unifiedNativeAd != null) {
unifiedNativeAd.destroy();
}
if (nativeCustomTemplateAd != null) {
nativeCustomTemplateAd.destroy();
}
super.onDestroy();
}
Expand Down
Expand Up @@ -202,6 +202,12 @@ private void refreshAd() {
// OnUnifiedNativeAdLoadedListener implementation.
@Override
public void onUnifiedNativeAdLoaded(UnifiedNativeAd unifiedNativeAd) {
// If this callback occurs after the activity is destroyed, you must call
// destroy and return or you may get a memory leak.
if (isDestroyed()) {
unifiedNativeAd.destroy();
return;
}
// You must call destroy on old ads when you are done with them,
// otherwise you will have a memory leak.
if (nativeAd != null) {
Expand Down
Expand Up @@ -59,6 +59,9 @@ public class DFPCustomControlsFragment extends Fragment {
private CheckBox contentAdsCheckbox;
private CheckBox customTemplateAdsCheckbox;
private CustomControlsView customControlsView;
private NativeAppInstallAd nativeAppInstallAd;
private NativeContentAd nativeContentAd;
private NativeCustomTemplateAd nativeCustomTemplateAd;

public DFPCustomControlsFragment() {
}
Expand Down Expand Up @@ -91,6 +94,23 @@ public void onClick(View unusedView) {
refreshAd();
}

@Override
public void onDestroy() {
if (nativeAppInstallAd != null) {
nativeAppInstallAd.destroy();
nativeAppInstallAd = null;
}
if (nativeContentAd != null) {
nativeContentAd.destroy();
nativeContentAd = null;
}
if (nativeCustomTemplateAd != null) {
nativeCustomTemplateAd.destroy();
nativeCustomTemplateAd = null;
}
super.onDestroy();
}

/**
* Populates a {@link NativeAppInstallAdView} object with data from a given
* {@link NativeAppInstallAd}.
Expand Down Expand Up @@ -298,6 +318,14 @@ private void refreshAd() {
new NativeCustomTemplateAd.OnCustomTemplateAdLoadedListener() {
@Override
public void onCustomTemplateAdLoaded(NativeCustomTemplateAd ad) {
if (isDetached()) {
ad.destroy();
return;
}
if (nativeCustomTemplateAd != null) {
nativeCustomTemplateAd.destroy();
}
nativeCustomTemplateAd = ad;
FrameLayout frameLayout = getView().findViewById(R.id.fl_adplaceholder);
View adView = getLayoutInflater()
.inflate(R.layout.ad_simple_custom_template, null);
Expand All @@ -320,6 +348,14 @@ public void onCustomClick(NativeCustomTemplateAd ad, String s) {
builder.forAppInstallAd(new NativeAppInstallAd.OnAppInstallAdLoadedListener() {
@Override
public void onAppInstallAdLoaded(NativeAppInstallAd ad) {
if (isDetached()) {
ad.destroy();
return;
}
if (nativeAppInstallAd != null) {
nativeAppInstallAd.destroy();
}
nativeAppInstallAd = ad;
FrameLayout frameLayout = getView().findViewById(R.id.fl_adplaceholder);
NativeAppInstallAdView adView = (NativeAppInstallAdView) getLayoutInflater()
.inflate(R.layout.ad_app_install, null);
Expand All @@ -334,6 +370,14 @@ public void onAppInstallAdLoaded(NativeAppInstallAd ad) {
builder.forContentAd(new NativeContentAd.OnContentAdLoadedListener() {
@Override
public void onContentAdLoaded(NativeContentAd ad) {
if (isDetached()) {
ad.destroy();
return;
}
if (nativeContentAd != null) {
nativeContentAd.destroy();
}
nativeContentAd = ad;
FrameLayout frameLayout = getView().findViewById(R.id.fl_adplaceholder);
NativeContentAdView adView = (NativeContentAdView) getLayoutInflater()
.inflate(R.layout.ad_content, null);
Expand Down Expand Up @@ -372,3 +416,4 @@ public void onAdFailedToLoad(int errorCode) {




2 changes: 1 addition & 1 deletion kotlin/admanager/NativeAdsExample/app/build.gradle
Expand Up @@ -9,7 +9,7 @@ android {
buildToolsVersion '29.0.0'
defaultConfig {
applicationId "com.google.android.gms.example.nativeadsexample"
minSdkVersion 16
minSdkVersion 17
targetSdkVersion 29
versionCode 1
versionName "1.0"
Expand Down
Expand Up @@ -32,7 +32,8 @@ import java.util.*
const val AD_MANAGER_AD_UNIT_ID = "/6499/example/native"
const val SIMPLE_TEMPLATE_ID = "10104090"

var currentNativeAd: UnifiedNativeAd? = null
var currentUnifiedNativeAd: UnifiedNativeAd? = null
var currentCustomTemplateAd: NativeCustomTemplateAd? = null

/**
* A simple activity class that displays native ad formats.
Expand Down Expand Up @@ -60,11 +61,6 @@ class MainActivity : AppCompatActivity() {
* @param adView the view to be populated
*/
private fun populateUnifiedNativeAdView(nativeAd: UnifiedNativeAd, adView: UnifiedNativeAdView) {
// You must call destroy on old ads when you are done with them,
// otherwise you will have a memory leak.
currentNativeAd?.destroy()
currentNativeAd = nativeAd

// Set the media view.
adView.mediaView = adView.findViewById<MediaView>(R.id.ad_media)

Expand Down Expand Up @@ -248,6 +244,16 @@ class MainActivity : AppCompatActivity() {

if (requestUnifiedNativeAds) {
builder.forUnifiedNativeAd { unifiedNativeAd ->
// If this callback occurs after the activity is destroyed, you must call
// destroy and return or you may get a memory leak.
if (isDestroyed) {
unifiedNativeAd.destroy()
return@forUnifiedNativeAd
}
// You must call destroy on old ads when you are done with them,
// otherwise you will have a memory leak.
currentUnifiedNativeAd?.destroy()
currentUnifiedNativeAd = unifiedNativeAd
val adView = layoutInflater
.inflate(R.layout.ad_unified, null) as UnifiedNativeAdView
populateUnifiedNativeAdView(unifiedNativeAd, adView)
Expand All @@ -260,6 +266,16 @@ class MainActivity : AppCompatActivity() {
builder.forCustomTemplateAd(SIMPLE_TEMPLATE_ID,
{
ad: NativeCustomTemplateAd ->
// If this callback occurs after the activity is destroyed, you must call
// destroy and return or you may get a memory leak.
if (isDestroyed) {
ad.destroy()
return@forCustomTemplateAd
}
// You must call destroy on old ads when you are done with them,
// otherwise you will have a memory leak.
currentCustomTemplateAd?.destroy()
currentCustomTemplateAd = ad
val frameLayout = findViewById<FrameLayout>(R.id.ad_frame)
val adView = layoutInflater
.inflate(R.layout.ad_simple_custom_template, null)
Expand Down Expand Up @@ -299,7 +315,8 @@ class MainActivity : AppCompatActivity() {
}

override fun onDestroy() {
currentNativeAd?.destroy()
currentUnifiedNativeAd?.destroy()
currentCustomTemplateAd?.destroy()
super.onDestroy()
}
}

0 comments on commit c9c358e

Please sign in to comment.