Do you need to explicitly call pause, resume, destroy for an AdView?
Asked Answered
S

3

16

Is the AdView automatically tied to the activity life cycle or do you have to explicitly call the pause, resume, destroy events? Does it depend on the size of the AdView? I'm using banner ads.

I couldn't find a lot of code samples of other people doing this and the main Android help article doesn't mention it (they just load the ad in onCreate and don't do anything else with it).

https://developers.google.com/android/reference/com/google/android/gms/ads/AdView (code example includes pause/resume/destroy and it mentions we "should call these methods" in the method notes, but doesn't elaborate).

https://developers.google.com/admob/android/banner (does not mention the need to pause/resume/destroy).

http://thetechnocafe.com/a-complete-guide-to-integrating-admob-in-your-android-app/ (pauses and destroys video ads in the code, but doesn't mention why or give any explanation)

<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

    <!-- app content -->

    <com.google.android.gms.ads.AdView
            xmlns:ads="http://schemas.android.com/apk/res-auto"
            android:id="@+id/myAdView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            ads:adSize="BANNER"
            ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
    </com.google.android.gms.ads.AdView>
</LinearLayout>
public class MainActivity extends AppCompatActivity {
    private AdView mAdView;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // ...

        mAdView = findViewById(R.id.myAdView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);
    }

    // do I need this code as well???
    @Override
    public void onResume() {
        mAdView.resume();
        super.onResume();
    }
    
    @Override
    public void onPause() {
        mAdView.pause();
        super.onPause();
    }
    
    @Override
    public void onDestroy() {
        mAdView.destroy();
        super.onDestroy();
    }
Serpentine answered 27/8, 2019 at 16:43 Comment(3)
Did you find the answer yourself? I am having the same problem and have no idea if this is necessary.Streptomycin
I'm calling pause, destroy, and resume just to be safe, but I have yet to receive a clear cut answer on this.Serpentine
hmm, ok thanks a lot. I think I will have to do the same :/Streptomycin
C
1

You Should always try to use pause, resume and destroy. As adView does background processing ( ad analytics ). That doesn't make a huge difference. but there is no harm in using them.

Adview.onPause() - Pauses any extra processing associated with this ad view. Adview.onResume() - Resumes an ad view after a previous call to pause(). Adview.destroy() - Destroy the Ad completely

This is what Android doc has to say Android Doc

Cymric answered 17/11, 2023 at 4:53 Comment(2)
and how to do it in Compose with AndroidView? we can use onRelease = { adView -> adView.destroy() }, but what about pause/resume?Heyday
You have to make custom lifecycle observer and act accordinglyCymric
G
1

Yes, we need to add these lines of code for saving amount of memory availability.

    @Override
    public void onResume() {
        super.onResume();
        mAdView.resume();
    }

you move resume() of adview below super constructor.

Gleanings answered 13/8, 2021 at 1:22 Comment(7)
Where did you find this info? Can you point out any docs?Serpentine
From google developers docs, Here is the link developers.google.com/android/reference/com/google/android/gms/…Gleanings
The doc you linked doesn't mention doing this to save memory or any reason why you do it. It's just a code example that does it, much like the one I linked in the original question.Serpentine
I thought you asked for why this code should be. Now it makes complicated because you asked for activity lifecycle. Understand the Activity Lifecycle in every stage Why we do onPause then onResume.Gleanings
you need to pause and resume ad because if you dont pause it banner will still get impressions if the app is in background which will cause admob limit (invalid traffic)Disenthrall
@HassanElkhalifte I just saw your comment now. Can you link to any official sources for your claim?Serpentine
@MayankSinghal no, I have not found an answer yetSerpentine
C
1

You Should always try to use pause, resume and destroy. As adView does background processing ( ad analytics ). That doesn't make a huge difference. but there is no harm in using them.

Adview.onPause() - Pauses any extra processing associated with this ad view. Adview.onResume() - Resumes an ad view after a previous call to pause(). Adview.destroy() - Destroy the Ad completely

This is what Android doc has to say Android Doc

Cymric answered 17/11, 2023 at 4:53 Comment(2)
and how to do it in Compose with AndroidView? we can use onRelease = { adView -> adView.destroy() }, but what about pause/resume?Heyday
You have to make custom lifecycle observer and act accordinglyCymric
A
-2

Its your choice but you don't really need to pause, resume or destroy it again and again with the android life cycle. Simple load the banner and interstitial ads in onCreate and call the interstitial ads in some click event and once the ad is closed by user simply reload the interstitial ads.

Here is the given example for interstitial ads

Call this function to load interstitial ad in onCreate

private void requestNewInterstitial() {
    AdRequest adRequest = new AdRequest.Builder()
            .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
            .build();

    mInterstitialAd.loadAd(adRequest);
}

Add this code where you want to load ad

if (mInterstitialAd.isLoaded()) {

                mInterstitialAd.show();

                mInterstitialAd.setAdListener(new AdListener() {
                    @Override
                    public void onAdClosed() {
                        requestNewInterstitial();
                        //Perform Your functionality here
                    }
                });
            }
            else {
                requestNewInterstitial();
                // Your functionality here as well in case ad was not loaded 
                //  before
            }
Ackack answered 6/9, 2019 at 11:32 Comment(1)
I'm not talking about interstitial ads. I'm talking about banner ads. Also, I want an explanation of WHY you don't need to pause / resume / destroy the ads with some credible source behind it. "You don't really need to" is not a good enough answer.Serpentine

© 2022 - 2024 — McMap. All rights reserved.