1
votes

I am currently including AdMob into some of my apps but am facing an issue. I try to display an interstitial when changing the activity as this would be the place where it wouldn't annoy users.

My initial understanding was, that displaying the Ad would put the activity into pause mode and resume it after the ad has been closed.

This assumption seems to be wrong, as with the below code, the activity gets switched directly, the toast is showing that an Ad should be displayed, but as long as I don't comment the startActivity(intent), I never see the ad.

I am loading the ad in onCreate, then try to display it in another void which is being triggered when a button has been clicked (and, of course, if it has finished loading by that point in time).

Sourcecode:

@Override
public void onCreate(Bundle SafedInstanceState)
{
    [...]
    LoadAd();
    [...]
}

public void ShowAd(){
    if (interstitial.isLoaded()) {
    interstitial.show();
    }
}

public void LoadAd(){
    String MY_AD_UNIT_ID=getResources().getString(R.string.AdID);
    // Create the interstitial.
    interstitial = new InterstitialAd(this);
    interstitial.setAdUnitId(MY_AD_UNIT_ID);

    // Create ad request.
    AdRequest adRequest = new AdRequest.Builder().build();

    // Begin loading your interstitial.
    interstitial.loadAd(adRequest);
}

public void SWITCHACTIVITY (View view) {
    ShowAd();
    Intent intent = new Intent (this, ANOTHERACTIVITY.class);
    startActivity(intent);
    finish();
}

Highly appreciate any input on what I'm doing wrong here or how I could achieve that the Ad gets displayed and the activity gets switched / closed AFTER the ad has been closed.

Thanks in advance!

1
Interstitial Ad wont be shown immediately after you load it. You can listener method of it to check if ad is loaded. But putting Ad between activity switch its bad user experience.virendrao
I know it won't be shown immediately, this is why it gets loaded onCreate but only displayed when desired. (working fine on another app) The switch of activity is (in this case) a pretty good natural break in the app as after this point, a special service for the customer will be made available. I did some surveys with a representative group of users and also took a look at analytics and indeed, this is the place to put it. Ads won't bother users that are not using this function in the app, but the ones who use it will support me and would also not mind being displayed with an ad here ;)Kai
best practice is to load your ads from ANOTHERACTIVITY.class and show it on loadedAmrut Bidri
This would lead to the result that the activity is showing up and then (depending on the connection speed) the already visible layout will be put to background and the Ad takes over focus. I would like to avoid this as THIS would indeed decrease user experience. The preferred way would be to have the Ad prior to switching. Think I'll move the switch out of this method and trigger it in an onAdClosed(), respectively in the else of the if (interstitial.isLoaded())Kai

1 Answers

3
votes

Okay, so I worked around it. Somewhat ugly, but it's doing exactly what I want it to do. Before calling ShowAd(), I set my intent and then trigger it after the ad has been closed:

public void ShowAd(){
        if (interstitial.isLoaded()) {
            interstitial.show();
            interstitial.setAdListener(new AdListener() {
            public void onAdClosed() {
                if (PostAdIntent != null)
                {
                    startActivity(PostAdIntent);
                    PostAdIntent = null;
                    finish();
                };
            }
            });

        } else
        {
            if (PostAdIntent != null)
                {
                startActivity(PostAdIntent);
                PostAdIntent = null;
                finish();
                };
        }
}