Skip to content

Commit

Permalink
recreate adview when adUnitID is already set
Browse files Browse the repository at this point in the history
the adUnitID can only be set once, and sometimes in unfortunate rerenders the prop is set twice, which caused a crash.
  • Loading branch information
koenpunt committed Aug 9, 2017
1 parent 45f1106 commit c02f32d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
Expand Up @@ -26,11 +26,22 @@

class ReactAdView extends ReactViewGroup {

private Context mContext;

protected AdView adView;

String adUnitID;
String[] testDevices;

public ReactAdView(final Context context) {
super(context);
this.createAdView();
}

private void createAdView() {
if (this.adView != null) this.adView.destroy();

final Context context = getContext();
this.adView = new AdView(context);
this.adView.setAdListener(new AdListener() {
@Override
Expand Down Expand Up @@ -121,6 +132,12 @@ public void loadBanner() {
}

public void setAdUnitID(String adUnitID) {
if (this.adUnitID != null) {
// We can only set adUnitID once, so when it was previously set we have
// to recreate the view
this.createAdView();
}
this.adUnitID = adUnitID;
this.adView.setAdUnitId(adUnitID);
}

Expand Down
Expand Up @@ -30,13 +30,24 @@

class ReactPublisherAdView extends ReactViewGroup implements AppEventListener {

private Context mContext;

protected PublisherAdView adView;

String[] testDevices;
AdSize[] validAdSizes;
String adUnitID;
AdSize adSize;

public ReactPublisherAdView(final Context context) {
super(context);
this.createAdView();
}

private void createAdView() {
if (this.adView != null) this.adView.destroy();

final Context context = getContext();
this.adView = new PublisherAdView(context);
this.adView.setAppEventListener(this);
this.adView.setAdListener(new AdListener() {
Expand Down Expand Up @@ -145,6 +156,12 @@ public void loadBanner() {
}

public void setAdUnitID(String adUnitID) {
if (this.adUnitID != null) {
// We can only set adUnitID once, so when it was previously set we have
// to recreate the view
this.createAdView();
}
this.adUnitID = adUnitID;
this.adView.setAdUnitId(adUnitID);
}

Expand Down

0 comments on commit c02f32d

Please sign in to comment.