Kroenig
Kroenig

Reputation: 684

admob interstitial immediately show after button press?

I switched today to AdMob. My problem: The time for loading the Interstitial Ad is pretty high (up to 10 sconds).

My App: I have an Result Activity where the users stay for about 10 seconds. On this Activty is a Finish Button with which you get Back to the MainActivty (in my case "Home") where is another finish button to close the App.

My Question: Is it possible to load the Interstitial while the user is in the Result Activity and pop up immideantly if he hit the finish button?

My codes atm:

The OnCreate

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_result);


    this.interstitialAds = new InterstitialAd(this, "xxxMyCodeIDxxx");
    this.interstitialAds.setAdListener(this);}

and the onClick:

@Override
        public void onClick(View v) {

            Result.this.finish();
            startActivity(new Intent(Result.this, Home.class));
            AdRequest adr = new AdRequest();
                    adr.addTestDevice("xxxMyCodexxx");
                    interstitialAds.loadAd(adr);}

and the (unimportant) end:

 @Override
public void onDismissScreen(Ad arg0) {
}

@Override
public void onFailedToReceiveAd(Ad ad, ErrorCode error) {
    Toast.makeText(Result.this, "No ad found!", Toast.LENGTH_LONG)
    .show();
}

@Override
public void onLeaveApplication(Ad arg0) {
    // TODO Auto-generated method stub
}

@Override
public void onPresentScreen(Ad arg0) {
}

@Override
public void onReceiveAd(Ad arg0) {
    if (interstitialAds.isReady()) {
        interstitialAds.show();
    } else {
        Toast.makeText(Result.this, "Interstitial ad was not ready to be shown.", Toast.LENGTH_LONG)
        .show();
    }
}

If I use it like this the Interstitial loads and pops up if I click the finish button, but this takes up to 10 seconds. In this time everybody has clicked the 2nd exit button in the MainActivity to leave the App.

Thanks!

Upvotes: 1

Views: 4950

Answers (1)

William
William

Reputation: 20196

You should load the ad at an earlier time and then show it when they finish with the Result Activity.

I suggest loading the ad during Home#onCreate like so:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_result);


    this.interstitialAds = new InterstitialAd(this, "xxxMyCodeIDxxx");
    this.interstitialAds.setAdListener(this);

    final AdRequest adr = new AdRequest();
    adr.addTestDevice("xxxMyCodexxx");
    interstitialAds.loadAd(adr);
}

And when the button is clicked start ResultActivity such that you will be notified when it completes, and on completion show the ad (if it is available).

private static final int MY_REQUEST_CODE = 1;

@Override
public void onClick(View v) {
    // Result.this.finish(); // I don't know why you are calling this - it looks wrong.
    startActivityForResult(new Intent(Result.this, Home.class), MY_REQUEST_CODE);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode = MY_REQUEST_CODE) {
        if (interstitialAds.isReady()) {
            interstitialAds.show();
        } else {
            Toast.makeText(Result.this, "Interstitial ad was not ready to be shown.", Toast.LENGTH_LONG).show();
        }
    }
}

In ResultActivity you will need to call #setResult(someValue) at some point. I'd do it during onCreate.

The only problem you might face is if the Home Activity is garbage collected while ResultActivity is being displayed in which case the InterstitialAd will also be lost and need to be reloaded, but this is unlikely on most devices and with the limits you have placed on yourself I don't see another option.

Upvotes: 1

Related Questions