Search code examples
interstitialmopubmobile-ad-mediationskadnetwork

MoPub "onInterstitialFailed" adListener not work when offline


I am showing MoPub Interstitial ad between to activities.

When my device online/connect with internet. Then adListener work properly.This not work when device OFFLINE.

Situation: If ad not loaded or failed then I want to use Intent there to move next activity. But this MoPubAdListener not work when I am offline.

Interstitial mInterstitial.load() in onCreate method

When user press back button then Interstitial ad called

@Override
    public void onBackPressed() {
        try {
            customProgressDialogForAd.show(getSupportFragmentManager(),"before interstitial");
            funInterstitialLoadShow();
        } catch (Exception e){
            QuestionActivity.super.onBackPressed();
        }
        //super.onBackPressed();
    }

function of Interstitial call

private void funInterstitialLoadShow(){
        mInterstitial.setInterstitialAdListener(new MoPubInterstitial.InterstitialAdListener() {
            @Override
            public void onInterstitialLoaded(MoPubInterstitial moPubInterstitial) {
                Toast.makeText(QuestionActivity.this, "adloaded", Toast.LENGTH_SHORT).show();
                mInterstitial.show();
                customProgressDialogForAd.dismiss();
            }

            @Override
            public void onInterstitialFailed(MoPubInterstitial moPubInterstitial, MoPubErrorCode moPubErrorCode) {
                customProgressDialogForAd.dismiss();
                QuestionActivity.super.onBackPressed();
                
                // PROBLEM HERE: THIS fun now work when Device is OFFLINE(NO INTERNET);
            }

            @Override
            public void onInterstitialShown(MoPubInterstitial moPubInterstitial) {
            }

            @Override
            public void onInterstitialClicked(MoPubInterstitial moPubInterstitial) {
                
            }
            @Override
            public void onInterstitialDismissed(MoPubInterstitial moPubInterstitial) {
                customProgressDialogForAd.dismiss();
                QuestionActivity.super.onBackPressed();

            }
        });
    }

Problem: onInterstitialFailed not work when device not connected with internet. [on the other hand if internet is ON in device, then sdk works properly eg if we close ad then onInterstitialDismissed works]

Any Solution: Please


Solution

  • There is two way/method/type, to call MoPub's AdListener.

    1st way: Direct call adListener

    mInterstitial.setInterstitialAdListener(new MoPubInterstitial.InterstitialAdListener() { .... }
    

    2nd way: to call MoPub AdListener via implements in our Activity class

    public class QuestionActivity extends AppCompatActivity implements MoPubInterstitial.InterstitialAdListener { ... }
    

    Solution:

    You are not using mInterstitial.load() with adListener. Without adding this line of code with AdListener ,functions of Adlistener not works because this line is part of adListener.

         // ====this function (with moPub ad listener)== called when use click back button
    
        public void funInterstitialAdListener(){
            mInterstitial.setInterstitialAdListener(new MoPubInterstitial.InterstitialAdListener() {
                @Override
                public void onInterstitialLoaded(MoPubInterstitial moPubInterstitial) {
                    Log.d(TAG, "onInterstitialLoaded: funAd loaded");
                    customProgressDialogForAd.dismiss();
                    mInterstitial.show();
                }
    
                @Override
                public void onInterstitialFailed(MoPubInterstitial moPubInterstitial, MoPubErrorCode moPubErrorCode) {
                    customProgressDialogForAd.dismiss();
                    QuestionActivity.super.onBackPressed();
                    Log.d(TAG, "onInterstitialFailed: ad failed to load");
                }
    
                @Override
                public void onInterstitialShown(MoPubInterstitial moPubInterstitial) {
                }
    
                @Override
                public void onInterstitialClicked(MoPubInterstitial moPubInterstitial) {
    
                }
                @Override
                public void onInterstitialDismissed(MoPubInterstitial moPubInterstitial) {
                    Log.d(TAG, "onInterstitialDismissed: Done");
                    QuestionActivity.super.onBackPressed();
                }
            });
            // If we not use this mInterstitial.load() code, then function of AdListener not work. 
            mInterstitial.load();
            // Must use this upper line with adListener. This line is part of AdListener.
        }
    

    Happy Coding :)