Search code examples
androidandroid-fragmentsadmobinterstitial

How to stop delay showing Interstitial ads in Tabs?


I created an android app with 3 tabs(Fragments).

see image >>>

I added Interstitial ads in Search Tab. It means when I click Search tab Interstitial ad will pop up. But the problem is Interstitial ad load after 3 sec. It's wrong with AdMob policy. How to correct it? I want to load ad without delay when clicking the Search tab.

Here is my code:

MainActivity.java

public class MainActivity extends AppCompatActivity {                                                

    public InterstitialAd mInterstitialAd;                                                           

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

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 BottomNavigationView bottomNav = findViewById(R.id.bottom_navigation);                       
        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,              
                new HomeFragment()).commit();                                                        
        bottomNav.setOnNavigationItemSelectedListener(navListener);                                  
    }                                                                                                


            private BottomNavigationView.OnNavigationItemSelectedListener navListener =              
            new BottomNavigationView.OnNavigationItemSelectedListener() {                            
                @Override                                                                            
                public boolean onNavigationItemSelected(@NonNull MenuItem item) {                    
                    Fragment selectedFragment = null;                                                

                    switch (item.getItemId()) {                                                      
                        case R.id.nav_home:                                                          
                            selectedFragment = new HomeFragment();                                   
                            break;                                                                   
                        case R.id.nav_favorites:                                                     
                            selectedFragment = new FavoritesFragment();                              
                            break;                                                                   
                        case R.id.nav_search:                                                        
                            selectedFragment = new SearchFragment();                                 
                            break;                                                                   
                    }                                                                                                    getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,  
                            selectedFragment).commit();                                                                                                            
                    return true;                                                                     
                }                                                                                    
            };                                                                                       



//display code                                                                                       
private void displayInterstitial() {                                                                 

    mInterstitialAd.setAdListener(new AdListener() {                                                 
        public void onAdLoaded() {                                                                   
            if (mInterstitialAd.isLoaded()) {                                                        
                mInterstitialAd.show();                                                              
            }                                                                                        
        }                                                                                            
    });                                                                                              
}                                                                                                    
// display code end                                                                                  

}                             

SearchFragment.java

public class SearchFragment extends Fragment {
    public InterstitialAd mInterstitialAd;
    public static ViewPager viewPager;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.fragment_search, container, false);

      //fragment Interstitial ad code start
        final AdRequest adRequest = new AdRequest.Builder().build();
        mInterstitialAd = new InterstitialAd(getActivity());
        mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
        mInterstitialAd.loadAd(adRequest);
        mInterstitialAd.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                super.onAdLoaded();
                if(mInterstitialAd.isLoaded()){
                    mInterstitialAd.show();
                }
            }
            @Override
            public void onAdClosed() {
            }
        });
        //fragment Interstitial ad code end


        return v;
    }
}

Solution

  • It's nearly impossible for an InterstitialAd to be loaded without any delay, due to reasons such as network speed, network connectivity, Admob's servers, and etc.

    That's why, InterstitialAds are meant to be loaded first, then shown when you want it to be shown.

    Currently, you're loading an InterstitialAd in the onCreateView() method of your SearchFragment, then showing it immediately once it's loaded. This is actually the wrong approach.

    Instead, you should load the ad much earlier, such as in the onCreate() method of your MainActivity. Then, in the switch case for SearchFragment check if the InterstitialAd is loaded. If it is, show it.

    Here's what I mean:

    public class MainActivity extends AppCompatActivity {                                                
    
    public InterstitialAd mInterstitialAd;                                                           
    
    @Override                                                                                        
    protected void onCreate(Bundle savedInstanceState) {                                             
        super.onCreate(savedInstanceState);                                                          
        setContentView(R.layout.activity_main);                                                      
    
        loadInterstitial();
    
        BottomNavigationView bottomNav = findViewById(R.id.bottom_navigation);                       
        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,              
                new HomeFragment()).commit();                                                        
        bottomNav.setOnNavigationItemSelectedListener(navListener);                                  
    }                                                                                                
    
    
    private BottomNavigationView.OnNavigationItemSelectedListener navListener = new BottomNavigationView.OnNavigationItemSelectedListener() {                            
        @Override                                                                            
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {                    
            Fragment selectedFragment = null;                                                
            switch (item.getItemId()) {                                                      
                case R.id.nav_home:                                                          
                    selectedFragment = new HomeFragment();                                   
                    break;                                                                   
                case R.id.nav_favorites:                                                     
                    selectedFragment = new FavoritesFragment();                              
                    break;                                                                   
                case R.id.nav_search:                                                        
                    selectedFragment = new SearchFragment();     
                    displayInterstitial();
                    break;                                                                   
            }                                                                                                    
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,  
                            selectedFragment).commit();                                                                                                            
            return true;                                                                     
        }                                                                                    
    };                                                                                       
    
    private void loadInterstitial(){
        mInterstitialAd = new InterstitialAd(this);
        mInterstitialAd.setAdUnitId("ADMOB_INTERSTITIAL_ID");
        mInterstitialAd.loadAd(new AdRequest.Builder().build());
        mInterstitialAd.setAdListener(new AdListener() {
            @Override
            public void onAdClosed() {
                mInterstitialAd.loadAd(new AdRequest.Builder().build());
            }
        });
    }
    
    private void displayInterstitial() {                                                                 
        if (mInterstitialAd.isLoaded()) {                                                        
            mInterstitialAd.show();                                                              
        }                                                                                               
    }                                                                                                       
    

    You should also load the next InterstitialAd when the Ad is closed, so it'll be ready for the next time SearchFragment is pressed.

    Also, don't paste your Admob Unit ID in the code you show here. That's not info you should share.