k.james
k.james

Reputation: 29

How to stop interstitial ads showing up after app exit?

My app shows interstitial ads in the app and sometimes after the app is closed because of slow internet speed. Showing interstitial ad after the app is closed violates admob rules. How can i stop my interstitial ads from showing ads after app is closed ??

public class des extends AppCompatActivity {

    ImageButton imageView15;
    InterstitialAd mInterstitialAd;
    private InterstitialAd interstitial;

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

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

                   // Prepare the Interstitial Ad
        interstitial = new InterstitialAd(des.this);
// Insert the Ad Unit ID
        interstitial.setAdUnitId(getString(R.string.intertitial_id));

        interstitial.loadAd(adRequest);
// Prepare an Interstitial Ad Listener
        interstitial.setAdListener(new AdListener() {
            public void onAdLoaded() {
                // Call displayInterstitial() function
                displayInterstitial();
            }
        });


        imageView15 = (ImageButton) findViewById(R.id.imageView15);
        imageView15.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intentLoadNewActivity = new Intent(des.this,vid.class);
                startActivity(intentLoadNewActivity);
            }
        });


    }

    public void displayInterstitial() {
// If Ads are loaded, show Interstitial else show nothing.
        if (interstitial.isLoaded()) {
            interstitial.show();
        }
    }


}

Upvotes: 2

Views: 3992

Answers (4)

grantespo
grantespo

Reputation: 2269

Or, you could use: system.exit(0); whenever a user wants to exit your app as long as your app doesn't contain any service classes

Upvotes: 0

Mehul Gajjar
Mehul Gajjar

Reputation: 431

you need to destroy your ad on Activity finish or (Exit application)

try this

@Override
public void onDestroy()
{
    super.onDestroy();
    if (mInterstitialAd != null) {
        mInterstitialAd.destroy();
    }
}

Upvotes: -1

Nika Kurdadze
Nika Kurdadze

Reputation: 2512

You can keep track of the activity lifecycle and then check if the activity is running prior to showing the ad.

  1. Create an instance variable :

    private boolean isRunning;
    
  2. Keep track of the activity lifecycle :

    @Override
    protected void onStart() {
        super.onStart();
        isRunning = true;
    }
    
    @Override
    protected void onStop() {
        super.onStop();
        isRunning = false;
    } 
    
  3. Check if the activity is running before showing the ad :

    public void displayInterstitial() {
        // If Ads are loaded and the activity is running, show Interstitial else show nothing.
        if (isRunning && interstitial.isLoaded()) {
            interstitial.show();
        }
    }
    

Upvotes: 3

Arpan Sharma
Arpan Sharma

Reputation: 2162

The problem is that you are showing the add in OnAdLoaded which is not advised.This results in bad behaviour and intercepts when user is doing some other work.SO you should load your ad when the activity starts and then you should show it at a time where user is not effected by checking if it is loaded. This way you ad wont be shown after user is done with your app and your problem will be solved.

Upvotes: 3

Related Questions