Abhijit
Abhijit

Reputation: 370

Exception during overriding onStop() to show ad

I want to show an interstitial ad during user presses back/home or recent app button. As all action goes via onStop() method, so i did call the ad like this

@Override
protected void onStop() {
    if (mInterstitialAd.isLoaded()) {
        mInterstitialAd.show();         
        mInterstitialAd.setAdListener(new AdListener() {
            @Override
            public void onAdClosed() {
                // do nothing

            }
        });
    }else{
        super.onStop();
    }       
}

But I am getting exception `E/AndroidRuntime(26313): Caused by: android.app.SuperNotCalledException: Activity {package.name...} did not call through to super.onStop()

Intention is during those pressing button, if ad is loaded then show, else call super to proceed. What I am doing wrong ? Any other approach ?

Upvotes: 0

Views: 71

Answers (1)

Manish
Manish

Reputation: 1215

You need to call super.onStop in all cases. Change your code as below

    @Override
    protected void onStop () {
        if (mInterstitialAd.isLoaded()) {
            mInterstitialAd.show();
            mInterstitialAd.setAdListener(new AdListener() {
                @Override
                public void onAdClosed() {
                    // do nothing
                }
            });
        }
        super.onStop();
    }

Upvotes: 1

Related Questions