Skip to content

Commit

Permalink
fix(android): crash when activity context not available
Browse files Browse the repository at this point in the history
  • Loading branch information
Sygn53 authored Sep 9, 2023
1 parent 8b1a1b2 commit 7c89579
Showing 1 changed file with 26 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,16 @@ private BaseAdView initAdView(ReactNativeAdView reactViewGroup) {
}
BaseAdView adView;
if (ReactNativeGoogleMobileAdsCommon.isAdManagerUnit(reactViewGroup.getUnitId())) {
// in order to display the debug menu for GAM ads we need the activity context
// https://github.com/invertase/react-native-google-mobile-ads/issues/188
adView =
new AdManagerAdView(
Objects.requireNonNull(
((ReactContext) reactViewGroup.getContext()).getCurrentActivity()));
if (((ReactContext) reactViewGroup.getContext()).getCurrentActivity() != null) {
// in order to display the debug menu for GAM ads we need the activity context
// https://github.com/invertase/react-native-google-mobile-ads/issues/188
adView =
new AdManagerAdView(
Objects.requireNonNull(
((ReactContext) reactViewGroup.getContext()).getCurrentActivity()));
} else {
return null;
}
} else {
adView = new AdView(reactViewGroup.getContext());
}
Expand Down Expand Up @@ -255,24 +259,25 @@ private void requestAd(ReactNativeAdView reactViewGroup) {
}

BaseAdView adView = initAdView(reactViewGroup);
adView.setAdUnitId(unitId);

reactViewGroup.setIsFluid(false);
if (adView instanceof AdManagerAdView) {
if (sizes.contains(AdSize.FLUID)) {
reactViewGroup.setIsFluid(true);
((AdManagerAdView) adView).setAdSizes(AdSize.FLUID);
if (adView != null) {
adView.setAdUnitId(unitId);
reactViewGroup.setIsFluid(false);
if (adView instanceof AdManagerAdView) {
if (sizes.contains(AdSize.FLUID)) {
reactViewGroup.setIsFluid(true);
((AdManagerAdView) adView).setAdSizes(AdSize.FLUID);
} else {
((AdManagerAdView) adView).setAdSizes(sizes.toArray(new AdSize[0]));
}
if (manualImpressionsEnabled) {
((AdManagerAdView) adView).setManualImpressionsEnabled(true);
}
} else {
((AdManagerAdView) adView).setAdSizes(sizes.toArray(new AdSize[0]));
adView.setAdSize(sizes.get(0));
}
if (manualImpressionsEnabled) {
((AdManagerAdView) adView).setManualImpressionsEnabled(true);
}
} else {
adView.setAdSize(sizes.get(0));
}

adView.loadAd(request);
adView.loadAd(request);
}
}

private void sendEvent(ReactNativeAdView reactViewGroup, String type, WritableMap payload) {
Expand Down

0 comments on commit 7c89579

Please sign in to comment.