|
16 | 16 |
|
17 | 17 | package com.example.nextgenexample |
18 | 18 |
|
19 | | -import android.app.Activity |
20 | 19 | import android.app.Application |
21 | | -import android.os.Bundle |
22 | | -import androidx.fragment.app.FragmentActivity |
23 | | -import androidx.lifecycle.DefaultLifecycleObserver |
24 | | -import androidx.lifecycle.LifecycleOwner |
25 | | -import androidx.lifecycle.ProcessLifecycleOwner |
26 | | -import androidx.navigation.fragment.NavHostFragment |
27 | | -import androidx.preference.PreferenceManager |
28 | 20 | import com.example.nextgenexample.appopen.AppOpenAdManager |
29 | | -import com.example.nextgenexample.appopen.AppOpenFragment |
30 | 21 |
|
31 | | -/** Application class that initializes, loads and show ads when activities change states. */ |
32 | | -class MyApplication : |
33 | | - Application(), Application.ActivityLifecycleCallbacks, DefaultLifecycleObserver { |
34 | | - |
35 | | - private var currentActivity: Activity? = null |
| 22 | +class MyApplication : Application() { |
36 | 23 |
|
37 | 24 | override fun onCreate() { |
38 | 25 | super<Application>.onCreate() |
39 | | - registerActivityLifecycleCallbacks(this) |
40 | | - |
41 | | - ProcessLifecycleOwner.get().lifecycle.addObserver(this) |
42 | | - } |
43 | | - |
44 | | - /** |
45 | | - * DefaultLifecycleObserver method that shows the app open ad when the app moves to foreground. |
46 | | - */ |
47 | | - override fun onStart(owner: LifecycleOwner) { |
48 | | - currentActivity?.let { activity -> |
49 | | - // Show app open ad if the switch to enable app open ads on all starts is on, or if returning |
50 | | - // to the AppOpenFragment. |
51 | | - |
52 | | - val isAppOpenFragment = |
53 | | - ((activity as? FragmentActivity) |
54 | | - ?.supportFragmentManager |
55 | | - ?.findFragmentById(R.id.nav_host_fragment_content_main) as? NavHostFragment) |
56 | | - ?.childFragmentManager |
57 | | - ?.primaryNavigationFragment is AppOpenFragment |
58 | | - |
59 | | - if ( |
60 | | - PreferenceManager.getDefaultSharedPreferences(activity) |
61 | | - .getBoolean(AppOpenFragment.KEY_SHOW_APP_OPEN_AD_ON_ALL_STARTS, false) || |
62 | | - isAppOpenFragment |
63 | | - ) { |
64 | | - AppOpenAdManager.showAdIfAvailable(activity, null) |
65 | | - return |
66 | | - } |
67 | | - } |
68 | | - } |
69 | | - |
70 | | - /** ActivityLifecycleCallback methods. */ |
71 | | - override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {} |
72 | | - |
73 | | - override fun onActivityStarted(activity: Activity) { |
74 | | - currentActivity = activity |
| 26 | + AppOpenAdManager.initialize(this) |
75 | 27 | } |
76 | | - |
77 | | - override fun onActivityResumed(activity: Activity) {} |
78 | | - |
79 | | - override fun onActivityPaused(activity: Activity) {} |
80 | | - |
81 | | - override fun onActivityStopped(activity: Activity) {} |
82 | | - |
83 | | - override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {} |
84 | | - |
85 | | - override fun onActivityDestroyed(activity: Activity) {} |
86 | 28 | } |
0 commit comments