File tree Expand file tree Collapse file tree 2 files changed +34
-0
lines changed
android/src/main/java/com/sbugert/rnadmob Expand file tree Collapse file tree 2 files changed +34
-0
lines changed Original file line number Diff line number Diff line change 2626
2727class 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
Original file line number Diff line number Diff line change 3030
3131class 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
You can’t perform that action at this time.
0 commit comments