13
13
import androidx .fragment .app .Fragment ;
14
14
import androidx .navigation .fragment .NavHostFragment ;
15
15
16
- import com .google .android .gms .ads .AdListener ;
17
16
import com .google .android .gms .ads .AdRequest ;
18
17
import com .google .android .gms .ads .AdView ;
19
- import com .google .android .gms .ads .InterstitialAd ;
18
+ import com .google .android .gms .ads .FullScreenContentCallback ;
20
19
import com .google .android .gms .ads .LoadAdError ;
21
20
import com .google .android .gms .ads .MobileAds ;
21
+ import com .google .android .gms .ads .interstitial .InterstitialAd ;
22
+ import com .google .android .gms .ads .interstitial .InterstitialAdLoadCallback ;
22
23
import com .google .samples .quickstart .admobexample .R ;
23
24
import com .google .samples .quickstart .admobexample .databinding .FragmentFirstBinding ;
24
25
@@ -48,63 +49,64 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
48
49
MobileAds .initialize (getContext ());
49
50
50
51
mAdView = binding .adView ;
51
- AdRequest adRequest = new AdRequest .Builder ().build ();
52
- mAdView .loadAd (adRequest );
53
-
54
- // AdMob ad unit IDs are not currently stored inside the google-services.json file.
55
- // Developers using AdMob can store them as custom values in a string resource file or
56
- // simply use constants. Note that the ad units used here are configured to return only test
57
- // ads, and should not be used outside this sample.
58
-
59
- // Create an InterstitialAd object. This same object can be re-used whenever you want to
60
- // show an interstitial.
61
- mInterstitialAd = new InterstitialAd (getContext ());
62
- mInterstitialAd .setAdUnitId (getString (R .string .interstitial_ad_unit_id ));
63
52
64
- mInterstitialAd .setAdListener (new AdListener () {
65
- @ Override
66
- public void onAdClosed () {
67
- requestNewInterstitial ();
68
- beginSecondActivity ();
69
- }
70
-
71
- @ Override
72
- public void onAdLoaded () {
73
- // Ad received, ready to display
74
- if (mLoadInterstitialButton != null ) {
75
- mLoadInterstitialButton .setEnabled (true );
76
- }
77
- }
78
-
79
- @ Override
80
- public void onAdFailedToLoad (LoadAdError error ) {
81
- Log .w (TAG , "onAdFailedToLoad:" + error .getMessage ());
82
- }
83
- });
53
+ requestNewInterstitial ();
84
54
85
55
mLoadInterstitialButton = binding .loadInterstitialButton ;
86
56
mLoadInterstitialButton .setOnClickListener (new View .OnClickListener () {
87
57
@ Override
88
58
public void onClick (View v ) {
89
- if (mInterstitialAd . isLoaded () ) {
90
- mInterstitialAd .show ();
59
+ if (mInterstitialAd != null ) {
60
+ mInterstitialAd .show (getActivity () );
91
61
} else {
92
62
beginSecondActivity ();
93
63
}
94
64
}
95
65
});
96
66
97
67
// Disable button if an interstitial ad is not loaded yet.
98
- mLoadInterstitialButton .setEnabled (mInterstitialAd . isLoaded () );
68
+ mLoadInterstitialButton .setEnabled (mInterstitialAd != null );
99
69
}
100
70
101
71
/**
102
72
* Load a new interstitial ad asynchronously.
103
73
*/
104
74
private void requestNewInterstitial () {
105
75
AdRequest adRequest = new AdRequest .Builder ().build ();
76
+ mAdView .loadAd (adRequest );
77
+
78
+ // AdMob ad unit IDs are not currently stored inside the google-services.json file.
79
+ // Developers using AdMob can store them as custom values in a string resource file or
80
+ // simply use constants. Note that the ad units used here are configured to return only test
81
+ // ads, and should not be used outside this sample.
82
+ InterstitialAd .load (getContext (), getString (R .string .interstitial_ad_unit_id ), adRequest , new InterstitialAdLoadCallback () {
83
+ @ Override
84
+ public void onAdLoaded (@ NonNull InterstitialAd interstitialAd ) {
85
+ super .onAdLoaded (interstitialAd );
86
+ mInterstitialAd = interstitialAd ;
106
87
107
- mInterstitialAd .loadAd (adRequest );
88
+ // Ad received, ready to display
89
+ if (mLoadInterstitialButton != null ) {
90
+ mLoadInterstitialButton .setEnabled (true );
91
+ }
92
+
93
+ mInterstitialAd .setFullScreenContentCallback (new FullScreenContentCallback () {
94
+ @ Override
95
+ public void onAdDismissedFullScreenContent () {
96
+ super .onAdDismissedFullScreenContent ();
97
+ requestNewInterstitial ();
98
+ beginSecondActivity ();
99
+ }
100
+ });
101
+ }
102
+
103
+ @ Override
104
+ public void onAdFailedToLoad (@ NonNull LoadAdError loadAdError ) {
105
+ super .onAdFailedToLoad (loadAdError );
106
+ mInterstitialAd = null ;
107
+ Log .w (TAG , "onAdFailedToLoad:" + loadAdError .getMessage ());
108
+ }
109
+ });
108
110
}
109
111
110
112
private void beginSecondActivity () {
@@ -127,7 +129,7 @@ public void onResume() {
127
129
if (mAdView != null ) {
128
130
mAdView .resume ();
129
131
}
130
- if (! mInterstitialAd . isLoaded () ) {
132
+ if (mInterstitialAd == null ) {
131
133
requestNewInterstitial ();
132
134
}
133
135
}
0 commit comments