Search code examples
javaandroidadmobinterstitial

Interestitial Ads blocks app function in offline


My app has two ads :

  1. Banner
  2. Interstitial.

When I open my app the interestitial ad comes first(when data is on). When (data is off) the app doesn't functions properly like button onclick, etc. Only when the ad finished shows up my app works , or else it will not work in offline.

I want to run my app in offline and even if the ad not shows up my app should work. Hope You'll help me and the question will be helpful for others

My Project

MainActivity.Java

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    private InterstitialAd interstitial;
    private AdView mAdView;
    Button xxxx;

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

    //GOOGLE ADMOB FIREBASE ADS //

    //Interstitial//

    AdRequest adRequest2 = new AdRequest.Builder().build();
    interstitial = new InterstitialAd(MainActivity.this);
    interstitial.setAdUnitId("ca-app-pub-8736194125011489/4511020459");
    interstitial.loadAd(adRequest2);
    interstitial.setAdListener(new AdListener() {
        @Override
        public void onAdLoaded() {
            displayInterstitial();
        }
    });
}

private void displayInterstitial() {
    if (interstitial.isLoaded()) {
        interstitial.show();
    }

    //Banner Ad//

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

    //button//

    xxxx = (Button)findViewById(R.id.xxxx);
    xxxx.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent xxxx = new Intent(MainActivity.this, xxxx.class);
            startActivity(xxxx);
        }
    });

Manifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jobyreuben.yyyyyyyyy">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <activity android:name=".SplashScreen" >

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

     </activity>


    <activity android:name=".MainActivity"/>


    <activity
        android:name=".xxxx"
        android:configChanges="orientation"
        android:screenOrientation="portrait" />

    </application>
    </manifest>

Solution

  • According to your code, you're adding listener to button in displayInterstitial method that is only called when interstitialAd loaded by onAdLoaded() callback method so you got ads on screen first then button works when you close interstitialAd.

    And at the counter side when Internet is off, interstitialAd not loaded so onAdLoaded() -> displayInterstitial() not called and no listener added to your button.

    1. AndroidManifest.xml

    Inside application tag add AdActivity, if want to use Interstitial Ads

    <meta-data android:name="com.google.android.gms.version"
               android:value="@integer/google_play_services_version" />
    
    <activity android:name="com.google.android.gms.ads.AdActivity"
              android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
              android:theme="@android:style/Theme.Translucent" />
    
    1. MainActivity

      private InterstitialAd interstitial;
      private AdView mAdView;
      Button xxxx;
      
        @Override
        protected void onCreate (Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
      
            xxxx = (Button)findViewById(R.id.xxxx);
            xxxx.setOnClickListener(new View.OnClickListener() {
                  @Override
                 public void onClick(View v) {
                   Intent xxxx = new Intent(MainActivity.this, xxxx.class);
                   startActivity(xxxx);
                 }
            });
      
            mAdView = (AdView) findViewById(R.id.adView);
            AdRequest.Builder adRequestBuilder = new AdRequest.Builder();
            adRequestBuilder.addTestDevice(AdRequest.DEVICE_ID_EMULATOR);  // for testing only
            mAdView.loadAd(adRequestBuilder.build()); 
      
            interstitial = new InterstitialAd(this);
            interstitial.setAdUnitId("ca-app-pub-8736194125011489/4511020459");
            interstitial.setAdListener(new AdListener() {
                   @Override
                   public void onAdLoaded() {}
      
                   @Override
                   public void onAdClosed() {
                        loadIntersitialAd();
                   }
            });
      
            loadIntersitialAd();
        }
      
        private void loadIntersitialAd(){
      
            AdRequest interstitialRequest = new AdRequest.Builder().build();
            interstitial.loadAd(interstitialRequest);
        }
      
        public void displayInterstitial() {
           if (interstitial.isLoaded()) interstitial.show();
        }
      
        public boolean isInterstitialLoaded(){
          return interstitial.isLoaded();
        }
      
        @Override
        protected void onResume() {
          super.onResume();
          mAdView.resume();
        }
      
        @Override
        protected void onPause() {
          super.onPause();
          mAdView.pause();
        }
      
        @Override
        protected void onDestroy() {
          super.onDestroy();
          mAdView.destroy();
       }
      }
      

    Whenever you want to show Ad call displayInterstitial().

    EDIT

    Call displayInterstitial() from onAdLoaded() if you want to show Interstitial ad just after ad is loaded but this is not ideal way, loading Ad take some time so you'll get some delay between your Activity come in foreground and Interstitial ad display.

    interstitial.setAdListener(new AdListener() {
             @Override
             public void onAdLoaded() {
                  displayInterstitial();   
             }
    
             @Override
             public void onAdClosed() {
                  loadIntersitialAd();
             }
      });
    

    EDIT 2

    Don't load loadIntersitialAd() from onAdClosed() when you're calling displayInterstitial() from onAdLoaded() callback method because it creates recursive loop.

    interstitial.setAdListener(new AdListener() {
             @Override
             public void onAdLoaded() {
                  displayInterstitial();   
             }
    
             @Override
             public void onAdClosed() {
    
             }
      });