Skip to content

Commit c02f32d

Browse files
committed
recreate adview when adUnitID is already set
the adUnitID can only be set once, and sometimes in unfortunate rerenders the prop is set twice, which caused a crash.
1 parent 45f1106 commit c02f32d

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

android/src/main/java/com/sbugert/rnadmob/RNAdMobBannerViewManager.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,22 @@
2626

2727
class ReactAdView extends ReactViewGroup {
2828

29+
private Context mContext;
30+
2931
protected AdView adView;
32+
33+
String adUnitID;
3034
String[] testDevices;
3135

3236
public ReactAdView(final Context context) {
3337
super(context);
38+
this.createAdView();
39+
}
40+
41+
private void createAdView() {
42+
if (this.adView != null) this.adView.destroy();
43+
44+
final Context context = getContext();
3445
this.adView = new AdView(context);
3546
this.adView.setAdListener(new AdListener() {
3647
@Override
@@ -121,6 +132,12 @@ public void loadBanner() {
121132
}
122133

123134
public void setAdUnitID(String adUnitID) {
135+
if (this.adUnitID != null) {
136+
// We can only set adUnitID once, so when it was previously set we have
137+
// to recreate the view
138+
this.createAdView();
139+
}
140+
this.adUnitID = adUnitID;
124141
this.adView.setAdUnitId(adUnitID);
125142
}
126143

android/src/main/java/com/sbugert/rnadmob/RNPublisherBannerViewManager.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,24 @@
3030

3131
class ReactPublisherAdView extends ReactViewGroup implements AppEventListener {
3232

33+
private Context mContext;
34+
3335
protected PublisherAdView adView;
36+
3437
String[] testDevices;
3538
AdSize[] validAdSizes;
39+
String adUnitID;
3640
AdSize adSize;
3741

3842
public ReactPublisherAdView(final Context context) {
3943
super(context);
44+
this.createAdView();
45+
}
46+
47+
private void createAdView() {
48+
if (this.adView != null) this.adView.destroy();
49+
50+
final Context context = getContext();
4051
this.adView = new PublisherAdView(context);
4152
this.adView.setAppEventListener(this);
4253
this.adView.setAdListener(new AdListener() {
@@ -145,6 +156,12 @@ public void loadBanner() {
145156
}
146157

147158
public void setAdUnitID(String adUnitID) {
159+
if (this.adUnitID != null) {
160+
// We can only set adUnitID once, so when it was previously set we have
161+
// to recreate the view
162+
this.createAdView();
163+
}
164+
this.adUnitID = adUnitID;
148165
this.adView.setAdUnitId(adUnitID);
149166
}
150167

0 commit comments

Comments
 (0)