1818import androidx .lifecycle .OnLifecycleEvent ;
1919import androidx .lifecycle .ProcessLifecycleOwner ;
2020
21- public class AppOpenManager implements LifecycleObserver , Application .ActivityLifecycleCallbacks {
21+ public class AppOpenManager implements LifecycleObserver , Application .ActivityLifecycleCallbacks {
2222
2323 private static final String LOG_TAG = "AppOpenManager" ;
2424 private static String AD_UNIT_ID ;
2525 private AppOpenAd appOpenAd = null ;
2626 private static boolean isShowingAds = false ;
2727
2828 private AppOpenAd .AppOpenAdLoadCallback loadCallback ;
29-
30- private Application myApplication ;
31-
29+ private final Application myApplication ;
3230 private Activity currentActivity ;
3331
3432 public AppOpenManager (Application myApplication , String adId ) {
@@ -47,64 +45,61 @@ public void onStart() {
4745 * Request an ad
4846 */
4947 public void fetchAd () {
50-
5148 if (isAdAvailable ()) {
5249 return ;
5350 }
51+
5452 loadCallback = new AppOpenAd .AppOpenAdLoadCallback () {
5553 @ Override
5654 public void onAdLoaded (@ NonNull AppOpenAd appOpenAd ) {
57- super .onAdLoaded (appOpenAd );
5855 AppOpenManager .this .appOpenAd = appOpenAd ;
56+ Log .d (LOG_TAG , "Ad was loaded." );
5957 }
6058
6159 @ Override
6260 public void onAdFailedToLoad (@ NonNull LoadAdError loadAdError ) {
63- super .onAdFailedToLoad (loadAdError );
64- Log .d ("POENAD" , "onAdFailedToLoad: " + loadAdError .getMessage ());
61+ Log .d (LOG_TAG , "Ad failed to load: " + loadAdError .getMessage ());
6562 }
6663 };
67- AdRequest adRequest = getAdRequest ();
68- AppOpenAd .load (myApplication ,
69- AD_UNIT_ID , adRequest ,
70- AppOpenAd .APP_OPEN_AD_ORIENTATION_PORTRAIT , loadCallback );
7164
65+ AdRequest adRequest = getAdRequest ();
66+ AppOpenAd .load (myApplication , AD_UNIT_ID , adRequest , AppOpenAd .APP_OPEN_AD_ORIENTATION_PORTRAIT , loadCallback );
7267 }
7368
7469 public void showAdIfAvailable () {
7570 if (!isShowingAds && isAdAvailable ()) {
76- FullScreenContentCallback fullScreenContentCallback =
77- new FullScreenContentCallback () {
78- @ Override
79- public void onAdFailedToShowFullScreenContent ( @ NonNull AdError adError ) {
80- super . onAdFailedToShowFullScreenContent ( adError );
81- }
82-
83- @ Override
84- public void onAdShowedFullScreenContent () {
85- super . onAdShowedFullScreenContent () ;
86- isShowingAds = true ;
87- }
88-
89- @ Override
90- public void onAdDismissedFullScreenContent () {
91- super . onAdDismissedFullScreenContent () ;
92- AppOpenManager . this . appOpenAd = null ;
93- isShowingAds = false ;
94- fetchAd ();
95- }
96-
97- @ Override
98- public void onAdImpression () {
99- super . onAdImpression ( );
100- }
101- };
71+ FullScreenContentCallback fullScreenContentCallback = new FullScreenContentCallback () {
72+ @ Override
73+ public void onAdFailedToShowFullScreenContent ( @ NonNull AdError adError ) {
74+ isShowingAds = false ;
75+ Log . d ( LOG_TAG , "Ad failed to show: " + adError . getMessage () );
76+ }
77+
78+ @ Override
79+ public void onAdShowedFullScreenContent () {
80+ isShowingAds = true ;
81+ Log . d ( LOG_TAG , "Ad showed fullscreen content." ) ;
82+ }
83+
84+ @ Override
85+ public void onAdDismissedFullScreenContent () {
86+ AppOpenManager . this . appOpenAd = null ;
87+ isShowingAds = false ;
88+ Log . d ( LOG_TAG , "Ad was dismissed." ) ;
89+ fetchAd ();
90+ }
91+
92+ @ Override
93+ public void onAdImpression () {
94+ Log . d ( LOG_TAG , "Ad impression logged." );
95+ }
96+ };
10297
10398 appOpenAd .setFullScreenContentCallback (fullScreenContentCallback );
10499 appOpenAd .show (currentActivity );
105- }
106- else
100+ } else {
107101 fetchAd ();
102+ }
108103 }
109104
110105 /**
@@ -122,9 +117,7 @@ public boolean isAdAvailable() {
122117 }
123118
124119 @ Override
125- public void onActivityCreated (@ NonNull Activity activity , @ Nullable Bundle savedInstanceState ) {
126-
127- }
120+ public void onActivityCreated (@ NonNull Activity activity , @ Nullable Bundle savedInstanceState ) {}
128121
129122 @ Override
130123 public void onActivityStarted (@ NonNull Activity activity ) {
@@ -134,28 +127,188 @@ public void onActivityStarted(@NonNull Activity activity) {
134127 @ Override
135128 public void onActivityResumed (@ NonNull Activity activity ) {
136129 currentActivity = activity ;
137-
138130 }
139131
140132 @ Override
141- public void onActivityPaused (@ NonNull Activity activity ) {
142-
143- }
133+ public void onActivityPaused (@ NonNull Activity activity ) {}
144134
145135 @ Override
146136 public void onActivityStopped (@ NonNull Activity activity ) {
147-
137+ if (currentActivity == activity ) {
138+ currentActivity = null ;
139+ }
148140 }
149141
150142 @ Override
151- public void onActivitySaveInstanceState (@ NonNull Activity activity , @ NonNull Bundle outState ) {
152-
153- }
143+ public void onActivitySaveInstanceState (@ NonNull Activity activity , @ NonNull Bundle outState ) {}
154144
155145 @ Override
156146 public void onActivityDestroyed (@ NonNull Activity activity ) {
157- currentActivity = null ;
158-
147+ if (currentActivity == activity ) {
148+ currentActivity = null ;
149+ }
159150 }
160151}
161152
153+
154+ //package arrowwould.in.admobopenads;
155+ //
156+ //import android.app.Activity;
157+ //import android.app.Application;
158+ //import android.os.Bundle;
159+ //import android.util.Log;
160+ //
161+ //import com.google.android.gms.ads.AdError;
162+ //import com.google.android.gms.ads.AdRequest;
163+ //import com.google.android.gms.ads.FullScreenContentCallback;
164+ //import com.google.android.gms.ads.LoadAdError;
165+ //import com.google.android.gms.ads.appopen.AppOpenAd;
166+ //
167+ //import androidx.annotation.NonNull;
168+ //import androidx.annotation.Nullable;
169+ //import androidx.lifecycle.Lifecycle;
170+ //import androidx.lifecycle.LifecycleObserver;
171+ //import androidx.lifecycle.OnLifecycleEvent;
172+ //import androidx.lifecycle.ProcessLifecycleOwner;
173+ //
174+ //public class AppOpenManager implements LifecycleObserver, Application.ActivityLifecycleCallbacks {
175+ //
176+ // private static final String LOG_TAG = "AppOpenManager";
177+ // private static String AD_UNIT_ID;
178+ // private AppOpenAd appOpenAd = null;
179+ // private static boolean isShowingAds = false;
180+ //
181+ // private AppOpenAd.AppOpenAdLoadCallback loadCallback;
182+ //
183+ // private Application myApplication;
184+ //
185+ // private Activity currentActivity;
186+ //
187+ // public AppOpenManager(Application myApplication, String adId) {
188+ // AD_UNIT_ID = adId;
189+ // this.myApplication = myApplication;
190+ // this.myApplication.registerActivityLifecycleCallbacks(this);
191+ // ProcessLifecycleOwner.get().getLifecycle().addObserver(this);
192+ // }
193+ //
194+ // @OnLifecycleEvent(Lifecycle.Event.ON_START)
195+ // public void onStart() {
196+ // showAdIfAvailable();
197+ // }
198+ //
199+ // /**
200+ // * Request an ad
201+ // */
202+ // public void fetchAd() {
203+ //
204+ // if (isAdAvailable()) {
205+ // return;
206+ // }
207+ // loadCallback = new AppOpenAd.AppOpenAdLoadCallback() {
208+ // @Override
209+ // public void onAdLoaded(@NonNull AppOpenAd appOpenAd) {
210+ // super.onAdLoaded(appOpenAd);
211+ // AppOpenManager.this.appOpenAd = appOpenAd;
212+ // }
213+ //
214+ // @Override
215+ // public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
216+ // super.onAdFailedToLoad(loadAdError);
217+ // Log.d("POENAD", "onAdFailedToLoad: " + loadAdError.getMessage());
218+ // }
219+ // };
220+ // AdRequest adRequest = getAdRequest();
221+ // AppOpenAd.load(myApplication,
222+ // AD_UNIT_ID, adRequest,
223+ // AppOpenAd.APP_OPEN_AD_ORIENTATION_PORTRAIT, loadCallback);
224+ //
225+ // }
226+ //
227+ // public void showAdIfAvailable() {
228+ // if (!isShowingAds && isAdAvailable()) {
229+ // FullScreenContentCallback fullScreenContentCallback =
230+ // new FullScreenContentCallback() {
231+ // @Override
232+ // public void onAdFailedToShowFullScreenContent(@NonNull AdError adError) {
233+ // super.onAdFailedToShowFullScreenContent(adError);
234+ // }
235+ //
236+ // @Override
237+ // public void onAdShowedFullScreenContent() {
238+ // super.onAdShowedFullScreenContent();
239+ // isShowingAds = true;
240+ // }
241+ //
242+ // @Override
243+ // public void onAdDismissedFullScreenContent() {
244+ // super.onAdDismissedFullScreenContent();
245+ // AppOpenManager.this.appOpenAd = null;
246+ // isShowingAds = false;
247+ // fetchAd();
248+ // }
249+ //
250+ // @Override
251+ // public void onAdImpression() {
252+ // super.onAdImpression();
253+ // }
254+ // };
255+ //
256+ // appOpenAd.setFullScreenContentCallback(fullScreenContentCallback);
257+ // appOpenAd.show(currentActivity);
258+ // }
259+ // else
260+ // fetchAd();
261+ // }
262+ //
263+ // /**
264+ // * Creates and returns ad request.
265+ // */
266+ // private AdRequest getAdRequest() {
267+ // return new AdRequest.Builder().build();
268+ // }
269+ //
270+ // /**
271+ // * Utility method that checks if ad exists and can be shown.
272+ // */
273+ // public boolean isAdAvailable() {
274+ // return appOpenAd != null;
275+ // }
276+ //
277+ // @Override
278+ // public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) {
279+ //
280+ // }
281+ //
282+ // @Override
283+ // public void onActivityStarted(@NonNull Activity activity) {
284+ // currentActivity = activity;
285+ // }
286+ //
287+ // @Override
288+ // public void onActivityResumed(@NonNull Activity activity) {
289+ // currentActivity = activity;
290+ //
291+ // }
292+ //
293+ // @Override
294+ // public void onActivityPaused(@NonNull Activity activity) {
295+ //
296+ // }
297+ //
298+ // @Override
299+ // public void onActivityStopped(@NonNull Activity activity) {
300+ //
301+ // }
302+ //
303+ // @Override
304+ // public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle outState) {
305+ //
306+ // }
307+ //
308+ // @Override
309+ // public void onActivityDestroyed(@NonNull Activity activity) {
310+ // currentActivity = null;
311+ //
312+ // }
313+ //}
314+ //
0 commit comments