Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AdMob - Disable request onPause()

I have read few articles on trying to stop adView when the app is hidden/minimised but this crashes my app.

This is my code, adView.LoadAd... and adView.stopLoading both crashes the app on startup.

public class MainActivity extends Activity implements OnItemSelectedListener {
    @Override
    protected void onResume() {
        super.onResume();
        if (AdViewStarted = true) {
            adView.loadAd(new AdRequest());
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (AdViewStarted = true) {
            adView.destroy();
        }
    }

    [...]

    public class AdMob extends Activity {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            adView = new AdView(this, AdSize.BANNER,"12345678901234567890");
            LinearLayout layout = (LinearLayout) findViewById(R.id.adView);
            layout.addView(adView);
            adView.loadAd(new AdRequest());
            AdViewStarted = true;
        }

        @Override
        public void onDestroy() {
            if (adView != null) {
                adView.destroy();
            }
            super.onDestroy();
        }
    }
}

Thanks in advance

like image 298
KickAss Avatar asked Apr 03 '13 18:04

KickAss


1 Answers

Replace the if statements, you have to use two equals instead of one. Correct would be

if (AdViewStarted == true) {
        adView.destroy();
}

or better

if (AdViewStarted) {
        adView.destroy();
}

By the win, variable names are starting with a lowercase char.

Also, what are you trying in your onCreate?

This is correct (I think, if not, show me the layout xml file and LogCat):

LinearLayout adView = (LinearLayout) findViewById(R.id.adView);
adView.loadAd(new AdRequest());
AdViewStarted = true;
like image 141
Leandros Avatar answered Nov 08 '22 14:11

Leandros