-2

When I add interstitial in oncreate of activity (not main activity) ads loading each time user open that activity

I need a code that show interstitial once when open a activity and if this activity opens again and again doesn't show any ads

public class MapActivity extends ActionBarActivity
{
public static final String EXTRA_POI_ID = "poi_id";
public static final String EXTRA_POI_LATITUDE = "poi_latitude";
public static final String EXTRA_POI_LONGITUDE = "poi_longitude";


public static Intent newIntent(Context context)
{
    return new Intent(context, MapActivity.class);
}


public static Intent newIntent(Context context, long poiId, double poiLatitude, double poiLongitude)
{
    Intent intent = new Intent(context, MapActivity.class);

    // extras
    intent.putExtra(EXTRA_POI_ID, poiId);
    intent.putExtra(EXTRA_POI_LATITUDE, poiLatitude);
    intent.putExtra(EXTRA_POI_LONGITUDE, poiLongitude);

    return intent;
}


@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_map);
    setupActionBar();
    //Dgad.setTest(true);
    Dgad.showRandomPopup(MapActivity.this);

    // init analytics tracker
    ((CityGuideApplication) getApplication()).getTracker();
}


@Override
public void onStart()
{
    super.onStart();

    // analytics
    GoogleAnalytics.getInstance(this).reportActivityStart(this);
}


@Override
public void onResume()
{
    super.onResume();
}


@Override
public void onPause()
{
    super.onPause();
}


@Override
public void onStop()
{
    super.onStop();

    // analytics
    GoogleAnalytics.getInstance(this).reportActivityStop(this);
}


@Override
public void onDestroy()
{
    super.onDestroy();
}


@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    // action bar menu behaviour
    switch(item.getItemId())
    {
        case android.R.id.home:
            finish();
            return true;

        default:
            return super.onOptionsItemSelected(item);
    }
}


private void setupActionBar()
{
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    ActionBar bar = getSupportActionBar();
    bar.setDisplayUseLogoEnabled(false);
    bar.setDisplayShowTitleEnabled(true);
    bar.setDisplayShowHomeEnabled(true);
    bar.setDisplayHomeAsUpEnabled(true);
    bar.setHomeButtonEnabled(true);
}

}

saadat 68
  • 17
  • 1
  • 7

1 Answers1

0

I solved it doing this :

end_ad=new InterstitialAd(this);
end_ad.setAdUnitId(getResources().getString(R.string.banner_ad_unit_id));
end_ad.loadAd(new AdRequest.Builder().build());

When you want to controle it :

if(end_ad.isLoaded()){
   end_ad.show();
   Log.d(TAG,"SHOWING");
}
else{
   Log.d(TAG, "NOT SHOWING");
}

Otherwise, you can create a GlobalConstantClass

public GlobalClass{
 public static boolean Shown = false;
}

Then when you show the ADS for first time you can put this boolean to true:

GlobalClass.Shown = true;

And if you want to show it when you close the app in the onDestroy(), you set the variable to false

Then at the time you want to show the ADDS you have to ask for this boolean, for example :

if(!GlobalClass.Shown){
 //Show adds
 Dgad.showRandomPopup(MapActivity.this);
 GlobalClass.Shown=true;
}
else {
 //Do nothing
}
Skizo-ozᴉʞS ツ
  • 19,464
  • 18
  • 81
  • 148