Вопрос

I am developing an android app and would like showing a mopub interstitial full screen ad when user exits the app pressing back button.

I have tried it creating the interstitial ad and displaying it in onDestroy method. Something like that:

@Override
public void onDestroy(){
    this.interstitial = new MoPubInterstitial(this, MY_INTERSTITIAL_AD_UNIT_ID_HERE);
    this.interstitial.setInterstitialAdListener(this);
    this.interstitial.load();

    super.onDestroy();
}

// InterstitialAdListener method
@Override
public void onInterstitialLoaded(MoPubInterstitial interstitial) {
    if (interstitial.isReady()) {
        mInterstitial.show();
    } else {
        // Other code
    }
}

However, I am not destroying interstitial anywhere (mInterstitial.destroy();) since I don't know where can I do it, and therefore I am getting this error:

Activity com.myActivity has leaked IntentReceiver com.mopub.mobileads.MoPubView$1@41baffc0 that was originally registered here. Are you missing a call to unregisterReceiver()?
android.app.IntentReceiverLeaked: Activity com.myActivity has leaked IntentReceiver com.mopub.mobileads.MoPubView$1@41baffc0 that was originally registered here. Are you missing a call to unregisterReceiver()?

Although I am getting this error, the add is shown (I have tested it in many devices) and it seems it works well in all of them except sony.

How could I improve this code to show interstitial on exit??

Thanks!!

Это было полезно?

Решение 2

Finally, I found a new ad technology called Postitial that let me show mopub full screen ads when user exits the app in a natural way. You can see more information here: http://iqzone.com/mobile-advertising-solutions/case-studies/

Другие советы

you load interstitial while your ondestroy. when your activity gets destroyed after the interstitialload you are leaking. in ondestroy call interstitial.destroy().

what you probably want to do is handling onbackpressed. load interstitial oncreate, show it onbackpressed and destroy it ondestroy.

Old post but i was looking now for this issue and found this post.

Now if you got an error about "Activity has leaked window", remember to put finish() on onAdClosed and nowhere else

mInterstitialAd.setAdListener(new AdListener() {
            @Override
            public void onAdClosed() {
                finish();
                requestNewInterstitial();

            }
        });

This if you want close activity and show interstitial.

In onDestroy() make sure you destroy mopubview

    if (mMoPubView!=null) {
        mMoPubView.destroy();
    }

you can try to overload the onBackPressed method instead. this way, the onDestroy can still be used for cleaning up the interstitial.

in your Activity

@Override
public void onBackPressed(){
    if (interstitial.isReady()) {
        interstitial.show(); //this will show the interstitial if it's ready
    } else {
        super.onBackPressed(); //this will exit the program
    } 
}

then in your listener call finish() when the interstitial is returned (when fail or close)

@Override
public void onInterstitialFailed(MoPubInterstitial interstitial, MoPubErrorCode errorCode) {
    finish(); //this will exit the program if interstitial fail to load.
} 

@Override
public void onInterstitialDismissed(MoPubInterstitial interstitial) {
    finish(); //this will exit the program if interstitial is closed
}

of course you still need to call the destroy as per normal.

@Override
protected void onDestroy() {
    interstitial.destroy();
    super.onDestroy();
}

100% working solution, we need to call destroy function for both interstial and banner ads.

 @Override
    protected void onDestroy() {

        if(mopubInterstitial != null){
            mopubInterstitial.destroy();
        }
        if (mopubBannerPubView != null){
            mopubBannerPubView.destroy();
        }
        super.onDestroy();
    }
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top