I have written code to show interstitial ads in my android app. Its working fine but the problem is that ads are popping up so much. What could be the problem?? is it the same way user will see the ads too frequently?
MainActivity
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private ViewPager viewPager;
private DrawerLayout drawer;
private TabLayout tabLayout;
private String[] pageTitle = {"fragment1", "fragment2","Fragment3"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
InterstitialAd mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId(getResources().getString(R.string.admob_interstitial_id));
AdRequest adRequestInter = new AdRequest.Builder().build();
mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
mInterstitialAd.show();
}
@Override
public void onAdClosed()
{
AdRequest adRequest = new AdRequest.Builder()
.build();
mInterstitialAd.loadAd(adRequest);
}
});
mInterstitialAd.loadAd(adRequestInter);
viewPager = (ViewPager) findViewById(R.id.view_pager);
viewPager.setOffscreenPageLimit(3);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
drawer = (DrawerLayout) findViewById(R.id.drawerLayout);
setSupportActionBar(toolbar);
AppRate.with(this)
.setInstallDays(0)
.setLaunchTimes(3)
.setRemindInterval(2)
.monitor();
AppRate.showRateDialogIfMeetsConditions(this);
AppRate.with(this).clearAgreeShowDialog();
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar,
R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
tabLayout = (TabLayout) findViewById(R.id.tab_layout);
for (int i = 0; i < 3; i++) {
tabLayout.addTab(tabLayout.newTab().setText(pageTitle[i]));
}
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
assert navigationView != null;
navigationView.setNavigationItemSelectedListener(this);
navigationView.setItemIconTintList(null);
ViewPagerAdapter pagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(pagerAdapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
@Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.fr1) {
viewPager.setCurrentItem(0);
} else if (id == R.id.fr2) {
viewPager.setCurrentItem(1);
}else if (id == R.id.fr3) {
viewPager.setCurrentItem(2);
}
else if (id == R.id.Suggestion) {
Intent intent = new Intent(this, Suggestion.class);
startActivity(intent);
}
else if (id == R.id.Logic) {
Intent intent = new Intent(this, logicBehind.class);
startActivity(intent);
}
else if (id == R.id.close) {
finish();
}
drawer.closeDrawer(GravityCompat.START);
return true;
}
@Override
public void onBackPressed() {
boolean wentBack = false;
if (Fragment1.canGoBack()) {
wentBack = true;
Fragment1.goBack();
}
if (Fragment2.canGoBack()) {
wentBack = true;
Fragment2.goBack();
}
if (Fragment3.canGoBack()) {
wentBack = true;
Fragment3.goBack();
}
if (!wentBack) {
new AlertDialog.Builder(this)
.setTitle("Exit app")
.setMessage("Do you want to exit the app?")
.setIcon(R.mipmap.mainlogo)
.setPositiveButton("Yes", (dialog, which) -> super.onBackPressed())
.setNegativeButton("No", (dialog, which) -> {})
.show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
}
This is what I have tried. The code for the Interstitial ad is added in the onCreate method of MainActivity.java.
mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
mInterstitialAd.show();
}
@Override
public void onAdClosed()
{
AdRequest adRequest = new AdRequest.Builder()
.build();
mInterstitialAd.loadAd(adRequest);
}
});
mInterstitialAd.loadAd(adRequestInter);
As per the code that you've written, you call loadAd() in adClose(). So when you click on cross, the next ad is loaded. and you've called ad.show() in onAdLoaded. So as soon as the ad loads, it shows.. so this is like a continuous loop, the next ad will show on every ad closed.