简体   繁体   中英

Implementing interstitial admob for different activities

I have an activity inside my app which contain 25+ button. I use the interstitial ad for every one of them - it is a big stories app so it is not so annoying I think! - Anyway I am using this code

public void clkBtnPart1 (View v) {

        Intent intentP1 = new Intent(PartsActivity.this,
                part1.class);
        startActivity(intentP1);

        // Interstital Ad
        if (mPublisherInterstitialAd.isLoaded()) {
            mPublisherInterstitialAd.show();
        } else {
            Log.d("TAG", "The interstitial wasn't loaded yet.");
        }
        // End Int Ad
    }

and I repeated it for +25 time for every button - in the same activity - so I have big code and I thinks it may slow down my app

Q1: How can I optimize the ad code structure?

Q2: Is there any benefit of this line

else {
            Log.d("TAG", "The interstitial wasn't loaded yet.");
        }

or I can safely delete it?

The MAIN purpose to both questions are (1) Cleaner Code (2) Faster App

Brother, you are correct this is not the good practice You can set onClick Attribute in XML which is the same function for all and use code like this for short and easy to understand and read

switch (id){
        case R.id.button1 :
            Intent intentP1 = new Intent(PartsActivity.this,
                    part1.class);
            startActivity(intentP1);
            loadInterstitial();
            break;
        case R.id.button2 :
            Intent intentP2 = new Intent(PartsActivity.this,
                    part2.class);
            startActivity(intentP2);
            loadInterstitial();
            break;
        case R.id.button3 :
            Intent intentP3 = new Intent(PartsActivity.this,
                    part3.class);
            startActivity(intentP3);
            loadInterstitial();
            break;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM