diff --git a/admob/app/build.gradle b/admob/app/build.gradle index f5f1515ed..939a9c0bc 100644 --- a/admob/app/build.gradle +++ b/admob/app/build.gradle @@ -28,8 +28,8 @@ android { } buildFeatures { - viewBinding = true -} + viewBinding = true + } } dependencies { @@ -38,10 +38,10 @@ dependencies { implementation 'androidx.appcompat:appcompat:1.2.0' implementation 'com.google.android.material:material:1.2.1' implementation 'androidx.browser:browser:1.0.0' + implementation 'androidx.navigation:navigation-fragment-ktx:2.3.2' + implementation 'androidx.navigation:navigation-ui-ktx:2.3.2' - // [START gradle_play_config] implementation 'com.google.android.gms:play-services-ads:19.6.0' - // [END gradle_play_config] // Import the Firebase BoM (see: https://firebase.google.com/docs/android/learn-more#bom) implementation platform('com.google.firebase:firebase-bom:26.1.1') @@ -50,6 +50,7 @@ dependencies { // for Google Analytics. This is recommended, but not required. implementation 'com.google.firebase:firebase-analytics' + debugImplementation "androidx.fragment:fragment-testing:1.2.5" androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' androidTestImplementation 'androidx.test:rules:1.3.0' androidTestImplementation 'androidx.test:runner:1.3.0' diff --git a/admob/app/src/androidTest/java/com/google/samples/quickstart/admobexample/InterstitialAdTest.java b/admob/app/src/androidTest/java/com/google/samples/quickstart/admobexample/InterstitialAdTest.java index ad1b8e325..598c8cdb8 100644 --- a/admob/app/src/androidTest/java/com/google/samples/quickstart/admobexample/InterstitialAdTest.java +++ b/admob/app/src/androidTest/java/com/google/samples/quickstart/admobexample/InterstitialAdTest.java @@ -1,17 +1,13 @@ package com.google.samples.quickstart.admobexample; -import androidx.test.espresso.IdlingRegistry; +import androidx.fragment.app.testing.FragmentScenario; import androidx.test.espresso.ViewInteraction; import androidx.test.ext.junit.runners.AndroidJUnit4; -import androidx.test.rule.ActivityTestRule; import androidx.test.filters.LargeTest; -import com.google.samples.quickstart.admobexample.java.MainActivity; +import com.google.samples.quickstart.admobexample.kotlin.FirstFragment; -import org.junit.After; -import org.junit.Before; -import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; @@ -28,26 +24,9 @@ @RunWith(AndroidJUnit4.class) public class InterstitialAdTest { - private AdViewIdlingResource mAdResource; - - @Rule - public ActivityTestRule mActivityTestRule = new ActivityTestRule<>(MainActivity.class); - - @Before - public void setUp() { - mAdResource = new AdViewIdlingResource(mActivityTestRule.getActivity().getAdView()); - IdlingRegistry.getInstance().register(mAdResource); - } - - @After - public void tearDown() { - IdlingRegistry.getInstance().unregister(mAdResource); - } - @Test public void interstitialAdTest() { - // Wait for ad to load - mAdResource.setIsLoadingAd(true); + FragmentScenario fragment = FragmentScenario.launchInContainer(FirstFragment.class); // Confirm that banner ad appears onView(withId(R.id.adView)) @@ -66,7 +45,7 @@ public void interstitialAdTest() { closeInterstitialButton.perform(click()); // Confirm that we're on the second activity - onView(withText(R.string.second_activity_content)) + onView(withText(R.string.second_fragment_content)) .check(matches(isDisplayed())); } } diff --git a/admob/app/src/main/AndroidManifest.xml b/admob/app/src/main/AndroidManifest.xml index ccfe591f9..55e6d2563 100644 --- a/admob/app/src/main/AndroidManifest.xml +++ b/admob/app/src/main/AndroidManifest.xml @@ -35,6 +35,7 @@ + @@ -43,23 +44,6 @@ android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" android:theme="@android:style/Theme.Translucent" /> - - - - - - - diff --git a/admob/app/src/main/java/com/google/samples/quickstart/admobexample/java/FirstFragment.java b/admob/app/src/main/java/com/google/samples/quickstart/admobexample/java/FirstFragment.java new file mode 100644 index 000000000..7f38d5eb7 --- /dev/null +++ b/admob/app/src/main/java/com/google/samples/quickstart/admobexample/java/FirstFragment.java @@ -0,0 +1,154 @@ +package com.google.samples.quickstart.admobexample.java; + +import android.os.Bundle; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.Button; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.annotation.VisibleForTesting; +import androidx.fragment.app.Fragment; +import androidx.navigation.fragment.NavHostFragment; + +import com.google.android.gms.ads.AdListener; +import com.google.android.gms.ads.AdRequest; +import com.google.android.gms.ads.AdView; +import com.google.android.gms.ads.InterstitialAd; +import com.google.android.gms.ads.LoadAdError; +import com.google.android.gms.ads.MobileAds; +import com.google.samples.quickstart.admobexample.R; +import com.google.samples.quickstart.admobexample.databinding.FragmentFirstBinding; + +class FirstFragment extends Fragment { + + private static final String TAG = "MainActivity"; + private static final String TEST_APP_ID = "ca-app-pub-3940256099942544~3347511713"; + + private AdView mAdView; + private InterstitialAd mInterstitialAd; + private Button mLoadInterstitialButton; + private FragmentFirstBinding binding; + + @Nullable + @Override + public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { + binding = FragmentFirstBinding.inflate(inflater, container, false); + return binding.getRoot(); + } + + @Override + public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + checkIds(); + + // Initialize the Google Mobile Ads SDK + MobileAds.initialize(getContext()); + + mAdView = binding.adView; + AdRequest adRequest = new AdRequest.Builder().build(); + mAdView.loadAd(adRequest); + + // AdMob ad unit IDs are not currently stored inside the google-services.json file. + // Developers using AdMob can store them as custom values in a string resource file or + // simply use constants. Note that the ad units used here are configured to return only test + // ads, and should not be used outside this sample. + + // Create an InterstitialAd object. This same object can be re-used whenever you want to + // show an interstitial. + mInterstitialAd = new InterstitialAd(getContext()); + mInterstitialAd.setAdUnitId(getString(R.string.interstitial_ad_unit_id)); + + mInterstitialAd.setAdListener(new AdListener() { + @Override + public void onAdClosed() { + requestNewInterstitial(); + beginSecondActivity(); + } + + @Override + public void onAdLoaded() { + // Ad received, ready to display + if (mLoadInterstitialButton != null) { + mLoadInterstitialButton.setEnabled(true); + } + } + + @Override + public void onAdFailedToLoad(LoadAdError error) { + Log.w(TAG, "onAdFailedToLoad:" + error.getMessage()); + } + }); + + mLoadInterstitialButton = binding.loadInterstitialButton; + mLoadInterstitialButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + if (mInterstitialAd.isLoaded()) { + mInterstitialAd.show(); + } else { + beginSecondActivity(); + } + } + }); + + // Disable button if an interstitial ad is not loaded yet. + mLoadInterstitialButton.setEnabled(mInterstitialAd.isLoaded()); + } + + /** + * Load a new interstitial ad asynchronously. + */ + private void requestNewInterstitial() { + AdRequest adRequest = new AdRequest.Builder().build(); + + mInterstitialAd.loadAd(adRequest); + } + + private void beginSecondActivity() { + NavHostFragment.findNavController(this).navigate(R.id.action_FirstFragment_to_SecondFragment); + } + + /** Called when leaving the activity */ + @Override + public void onPause() { + if (mAdView != null) { + mAdView.pause(); + } + super.onPause(); + } + + /** Called when returning to the activity */ + @Override + public void onResume() { + super.onResume(); + if (mAdView != null) { + mAdView.resume(); + } + if (!mInterstitialAd.isLoaded()) { + requestNewInterstitial(); + } + } + + /** Called before the activity is destroyed */ + @Override + public void onDestroy() { + if (mAdView != null) { + mAdView.destroy(); + } + super.onDestroy(); + } + + @VisibleForTesting + public AdView getAdView() { + return mAdView; + } + + private void checkIds() { + if (TEST_APP_ID.equals(getString(R.string.admob_app_id))) { + Log.w(TAG, "Your admob_app_id is not configured correctly, please see the README"); + } + } +} diff --git a/admob/app/src/main/java/com/google/samples/quickstart/admobexample/java/MainActivity.java b/admob/app/src/main/java/com/google/samples/quickstart/admobexample/java/MainActivity.java index cd9b3a917..65eca5536 100644 --- a/admob/app/src/main/java/com/google/samples/quickstart/admobexample/java/MainActivity.java +++ b/admob/app/src/main/java/com/google/samples/quickstart/admobexample/java/MainActivity.java @@ -15,171 +15,17 @@ */ package com.google.samples.quickstart.admobexample.java; -import android.content.Intent; import android.os.Bundle; -import android.util.Log; -import android.view.View; -import android.widget.Button; - -import androidx.annotation.VisibleForTesting; import androidx.appcompat.app.AppCompatActivity; -import com.google.android.gms.ads.AdListener; -import com.google.android.gms.ads.AdRequest; -import com.google.android.gms.ads.AdView; -import com.google.android.gms.ads.InterstitialAd; -import com.google.android.gms.ads.MobileAds; import com.google.samples.quickstart.admobexample.R; -import com.google.samples.quickstart.admobexample.databinding.ActivityMainBinding; -// [SNIPPET load_banner_ad] -// Load an ad into the AdView. -// [START load_banner_ad] public class MainActivity extends AppCompatActivity { - private static final String TAG = "MainActivity"; - private static final String TEST_APP_ID = "ca-app-pub-3940256099942544~3347511713"; - - private AdView mAdView; - // [START_EXCLUDE] - private InterstitialAd mInterstitialAd; - private Button mLoadInterstitialButton; - private ActivityMainBinding binding; - // [END_EXCLUDE] - @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - // [START_EXCLUDE] - binding = ActivityMainBinding.inflate(getLayoutInflater()); - mAdView = binding.adView; - mLoadInterstitialButton = binding.loadInterstitialButton; - View layout = binding.getRoot(); - // [END_EXCLUDE] - setContentView(layout); - checkIds(); - - // Initialize the Google Mobile Ads SDK - MobileAds.initialize(this); - - mAdView = findViewById(R.id.adView); - AdRequest adRequest = new AdRequest.Builder().build(); - mAdView.loadAd(adRequest); - // [END load_banner_ad] - - // AdMob ad unit IDs are not currently stored inside the google-services.json file. - // Developers using AdMob can store them as custom values in a string resource file or - // simply use constants. Note that the ad units used here are configured to return only test - // ads, and should not be used outside this sample. - - // [START instantiate_interstitial_ad] - // Create an InterstitialAd object. This same object can be re-used whenever you want to - // show an interstitial. - mInterstitialAd = new InterstitialAd(this); - mInterstitialAd.setAdUnitId(getString(R.string.interstitial_ad_unit_id)); - // [END instantiate_interstitial_ad] - - // [START create_interstitial_ad_listener] - mInterstitialAd.setAdListener(new AdListener() { - @Override - public void onAdClosed() { - requestNewInterstitial(); - beginSecondActivity(); - } - - @Override - public void onAdLoaded() { - // Ad received, ready to display - // [START_EXCLUDE] - if (mLoadInterstitialButton != null) { - mLoadInterstitialButton.setEnabled(true); - } - // [END_EXCLUDE] - } - - @Override - public void onAdFailedToLoad(int i) { - // See https://goo.gl/sCZj0H for possible error codes. - Log.w(TAG, "onAdFailedToLoad:" + i); - } - }); - // [END create_interstitial_ad_listener] - - // [START display_interstitial_ad] - mLoadInterstitialButton = findViewById(R.id.loadInterstitialButton); - mLoadInterstitialButton.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - if (mInterstitialAd.isLoaded()) { - mInterstitialAd.show(); - } else { - beginSecondActivity(); - } - } - }); - // [END display_interstitial_ad] - - // Disable button if an interstitial ad is not loaded yet. - mLoadInterstitialButton.setEnabled(mInterstitialAd.isLoaded()); - } - - /** - * Load a new interstitial ad asynchronously. - */ - // [START request_new_interstitial] - private void requestNewInterstitial() { - AdRequest adRequest = new AdRequest.Builder() - .build(); - - mInterstitialAd.loadAd(adRequest); + setContentView(R.layout.activity_main); } - // [END request_new_interstitial] - private void beginSecondActivity() { - Intent intent = new Intent(this, SecondActivity.class); - startActivity(intent); - } - - // [START add_lifecycle_methods] - /** Called when leaving the activity */ - @Override - public void onPause() { - if (mAdView != null) { - mAdView.pause(); - } - super.onPause(); - } - - /** Called when returning to the activity */ - @Override - public void onResume() { - super.onResume(); - if (mAdView != null) { - mAdView.resume(); - } - if (!mInterstitialAd.isLoaded()) { - requestNewInterstitial(); - } - } - - /** Called before the activity is destroyed */ - @Override - public void onDestroy() { - if (mAdView != null) { - mAdView.destroy(); - } - super.onDestroy(); - } - // [END add_lifecycle_methods] - - @VisibleForTesting - public AdView getAdView() { - return mAdView; - } - - private void checkIds() { - if (TEST_APP_ID.equals(getString(R.string.admob_app_id))) { - Log.w(TAG, "Your admob_app_id is not configured correctly, please see the README"); - } - } } diff --git a/admob/app/src/main/java/com/google/samples/quickstart/admobexample/java/SecondActivity.java b/admob/app/src/main/java/com/google/samples/quickstart/admobexample/java/SecondActivity.java deleted file mode 100644 index a05afbde0..000000000 --- a/admob/app/src/main/java/com/google/samples/quickstart/admobexample/java/SecondActivity.java +++ /dev/null @@ -1,32 +0,0 @@ -/** - * Copyright Google Inc. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.google.samples.quickstart.admobexample.java; - -import android.os.Bundle; -import androidx.appcompat.app.AppCompatActivity; - -import com.google.samples.quickstart.admobexample.R; - - -public class SecondActivity extends AppCompatActivity { - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.second_activity); - } - -} diff --git a/admob/app/src/main/java/com/google/samples/quickstart/admobexample/java/SecondFragment.java b/admob/app/src/main/java/com/google/samples/quickstart/admobexample/java/SecondFragment.java new file mode 100644 index 000000000..360b797e0 --- /dev/null +++ b/admob/app/src/main/java/com/google/samples/quickstart/admobexample/java/SecondFragment.java @@ -0,0 +1,17 @@ +package com.google.samples.quickstart.admobexample.java; + +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import androidx.fragment.app.Fragment; +import com.google.samples.quickstart.admobexample.R; + +class SecondFragment extends Fragment { + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + return inflater.inflate(R.layout.fragment_second, container, false); + } + +} diff --git a/admob/app/src/main/java/com/google/samples/quickstart/admobexample/kotlin/FirstFragment.kt b/admob/app/src/main/java/com/google/samples/quickstart/admobexample/kotlin/FirstFragment.kt new file mode 100644 index 000000000..5bc68db36 --- /dev/null +++ b/admob/app/src/main/java/com/google/samples/quickstart/admobexample/kotlin/FirstFragment.kt @@ -0,0 +1,136 @@ +package com.google.samples.quickstart.admobexample.kotlin + +import android.os.Bundle +import android.util.Log +import androidx.fragment.app.Fragment +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import android.widget.Button +import androidx.navigation.fragment.findNavController +import com.google.android.gms.ads.AdListener +import com.google.android.gms.ads.AdRequest +import com.google.android.gms.ads.AdView +import com.google.android.gms.ads.InterstitialAd +import com.google.android.gms.ads.LoadAdError +import com.google.android.gms.ads.MobileAds +import com.google.samples.quickstart.admobexample.R +import com.google.samples.quickstart.admobexample.databinding.FragmentFirstBinding + +class FirstFragment : Fragment() { + + private var _binding: FragmentFirstBinding? = null + private val binding get() = _binding!! + private lateinit var interstitialAd: InterstitialAd + private lateinit var adView: AdView + private lateinit var loadInterstitialButton: Button + + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, + savedInstanceState: Bundle?): View? { + _binding = FragmentFirstBinding.inflate(inflater, container, false) + return binding.root + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + + adView = binding.adView + loadInterstitialButton = binding.loadInterstitialButton + + checkIds() + + // Initialize the Google Mobile Ads SDK + MobileAds.initialize(context) + + val adRequest = AdRequest.Builder().build() + + adView.loadAd(adRequest) + + // AdMob ad unit IDs are not currently stored inside the google-services.json file. + // Developers using AdMob can store them as custom values in a string resource file or + // simply use constants. Note that the ad units used here are configured to return only test + // ads, and should not be used outside this sample. + + // Create an InterstitialAd object. This same object can be re-used whenever you want to + // show an interstitial. + interstitialAd = InterstitialAd(context) + interstitialAd.adUnitId = getString(R.string.interstitial_ad_unit_id) + + interstitialAd.adListener = object : AdListener() { + override fun onAdClosed() { + requestNewInterstitial() + goToNextFragment() + } + + override fun onAdLoaded() { + // Ad received, ready to display + loadInterstitialButton.isEnabled = true + } + + override fun onAdFailedToLoad(error: LoadAdError) { + Log.w(TAG, "onAdFailedToLoad:${error.message}") + } + } + + loadInterstitialButton.setOnClickListener { + if (interstitialAd.isLoaded) { + interstitialAd.show() + } else { + goToNextFragment() + } + } + + // Disable button if an interstitial ad is not loaded yet. + loadInterstitialButton.isEnabled = interstitialAd.isLoaded + } + + /** + * Load a new interstitial ad asynchronously. + */ + private fun requestNewInterstitial() { + val adRequest = AdRequest.Builder().build() + + interstitialAd.loadAd(adRequest) + } + + private fun goToNextFragment() { + findNavController().navigate(R.id.action_FirstFragment_to_SecondFragment) + } + + /** Called when leaving the activity */ + override fun onPause() { + adView.pause() + super.onPause() + } + + /** Called when returning to the activity */ + override fun onResume() { + super.onResume() + adView.resume() + if (!interstitialAd.isLoaded) { + requestNewInterstitial() + } + } + + /** Called before the activity is destroyed */ + override fun onDestroy() { + adView.destroy() + super.onDestroy() + } + + private fun checkIds() { + if (TEST_APP_ID == getString(R.string.admob_app_id)) { + Log.w(TAG, "Your admob_app_id is not configured correctly, please see the README") + } + } + + companion object { + private const val TAG = "FirstFragment" + private const val TEST_APP_ID = "ca-app-pub-3940256099942544~3347511713" + } + + override fun onDestroyView() { + super.onDestroyView() + _binding = null + } +} \ No newline at end of file diff --git a/admob/app/src/main/java/com/google/samples/quickstart/admobexample/kotlin/MainActivity.kt b/admob/app/src/main/java/com/google/samples/quickstart/admobexample/kotlin/MainActivity.kt index 9f5d8a31a..d9d7bab6e 100644 --- a/admob/app/src/main/java/com/google/samples/quickstart/admobexample/kotlin/MainActivity.kt +++ b/admob/app/src/main/java/com/google/samples/quickstart/admobexample/kotlin/MainActivity.kt @@ -1,144 +1,13 @@ package com.google.samples.quickstart.admobexample.kotlin -import android.content.Intent import android.os.Bundle -import android.util.Log -import android.widget.Button import androidx.appcompat.app.AppCompatActivity -import com.google.android.gms.ads.AdListener -import com.google.android.gms.ads.AdRequest -import com.google.android.gms.ads.AdView -import com.google.android.gms.ads.InterstitialAd -import com.google.android.gms.ads.MobileAds import com.google.samples.quickstart.admobexample.R -import com.google.samples.quickstart.admobexample.databinding.ActivityMainBinding -// [SNIPPET load_banner_ad] -// Load an ad into the AdView. -// [START load_banner_ad] class MainActivity : AppCompatActivity() { - // [START_EXCLUDE] - private lateinit var binding: ActivityMainBinding - private lateinit var interstitialAd: InterstitialAd - private lateinit var adView: AdView - private lateinit var loadInterstitialButton: Button - // [END_EXCLUDE] - override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - // [START_EXCLUDE] - binding = ActivityMainBinding.inflate(layoutInflater) - adView = binding.adView - loadInterstitialButton = binding.loadInterstitialButton - val layout = binding.root - // [END_EXCLUDE] - setContentView(layout) - checkIds() - - // Initialize the Google Mobile Ads SDK - MobileAds.initialize(this) - - val adRequest = AdRequest.Builder().build() - - adView.loadAd(adRequest) - // [END load_banner_ad] - - // AdMob ad unit IDs are not currently stored inside the google-services.json file. - // Developers using AdMob can store them as custom values in a string resource file or - // simply use constants. Note that the ad units used here are configured to return only test - // ads, and should not be used outside this sample. - - // [START instantiate_interstitial_ad] - // Create an InterstitialAd object. This same object can be re-used whenever you want to - // show an interstitial. - interstitialAd = InterstitialAd(this) - interstitialAd.adUnitId = getString(R.string.interstitial_ad_unit_id) - // [END instantiate_interstitial_ad] - - // [START create_interstitial_ad_listener] - interstitialAd.adListener = object : AdListener() { - override fun onAdClosed() { - requestNewInterstitial() - beginSecondActivity() - } - - override fun onAdLoaded() { - // Ad received, ready to display - // [START_EXCLUDE] - loadInterstitialButton.isEnabled = true - // [END_EXCLUDE] - } - - override fun onAdFailedToLoad(i: Int) { - // See https://goo.gl/sCZj0H for possible error codes. - Log.w(TAG, "onAdFailedToLoad:$i") - } - } - // [END create_interstitial_ad_listener] - - // [START display_interstitial_ad] - loadInterstitialButton.setOnClickListener { - if (interstitialAd.isLoaded) { - interstitialAd.show() - } else { - beginSecondActivity() - } - } - // [END display_interstitial_ad] - - // Disable button if an interstitial ad is not loaded yet. - loadInterstitialButton.isEnabled = interstitialAd.isLoaded - } - - /** - * Load a new interstitial ad asynchronously. - */ - // [START request_new_interstitial] - private fun requestNewInterstitial() { - val adRequest = AdRequest.Builder() - .build() - - interstitialAd.loadAd(adRequest) - } - // [END request_new_interstitial] - - private fun beginSecondActivity() { - val intent = Intent(this, SecondActivity::class.java) - startActivity(intent) - } - - // [START add_lifecycle_methods] - /** Called when leaving the activity */ - public override fun onPause() { - adView.pause() - super.onPause() - } - - /** Called when returning to the activity */ - public override fun onResume() { - super.onResume() - adView.resume() - if (!interstitialAd.isLoaded) { - requestNewInterstitial() - } - } - - /** Called before the activity is destroyed */ - public override fun onDestroy() { - adView.destroy() - super.onDestroy() - } - // [END add_lifecycle_methods] - - private fun checkIds() { - if (TEST_APP_ID == getString(R.string.admob_app_id)) { - Log.w(TAG, "Your admob_app_id is not configured correctly, please see the README") - } - } - - companion object { - private const val TAG = "MainActivity" - private const val TEST_APP_ID = "ca-app-pub-3940256099942544~3347511713" + setContentView(R.layout.activity_main) } } diff --git a/admob/app/src/main/java/com/google/samples/quickstart/admobexample/kotlin/SecondActivity.kt b/admob/app/src/main/java/com/google/samples/quickstart/admobexample/kotlin/SecondActivity.kt deleted file mode 100644 index f927b3aef..000000000 --- a/admob/app/src/main/java/com/google/samples/quickstart/admobexample/kotlin/SecondActivity.kt +++ /dev/null @@ -1,13 +0,0 @@ -package com.google.samples.quickstart.admobexample.kotlin - -import android.os.Bundle -import androidx.appcompat.app.AppCompatActivity -import com.google.samples.quickstart.admobexample.R - -class SecondActivity : AppCompatActivity() { - - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - setContentView(R.layout.second_activity) - } -} diff --git a/admob/app/src/main/java/com/google/samples/quickstart/admobexample/kotlin/SecondFragment.kt b/admob/app/src/main/java/com/google/samples/quickstart/admobexample/kotlin/SecondFragment.kt new file mode 100644 index 000000000..e8f105587 --- /dev/null +++ b/admob/app/src/main/java/com/google/samples/quickstart/admobexample/kotlin/SecondFragment.kt @@ -0,0 +1,16 @@ +package com.google.samples.quickstart.admobexample.kotlin + +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.fragment.app.Fragment +import com.google.samples.quickstart.admobexample.R + +class SecondFragment : Fragment() { + + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { + return inflater.inflate(R.layout.fragment_second, container, false) + } + +} \ No newline at end of file diff --git a/admob/app/src/main/res/layout/activity_main.xml b/admob/app/src/main/res/layout/activity_main.xml index a19bae84e..086951728 100644 --- a/admob/app/src/main/res/layout/activity_main.xml +++ b/admob/app/src/main/res/layout/activity_main.xml @@ -1,50 +1,21 @@ - - - - -