AdMob - Disable onPause () request

I read several articles about trying to stop adView when the application is hidden / minimized, but this leads to the failure of my application.

This is my code, adView.LoadAd ... and adView.stopLoading, which causes the application to crash 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(); } } } 

Thank you in advance

+4
source share
1 answer

Replace the if statements, you should use two equalities instead of one. It would be right

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

or better

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

As a result of winning, variable names begin with the lowercase char.

Also, what are you trying to use in onCreate ?

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

 LinearLayout adView = (LinearLayout) findViewById(R.id.adView); adView.loadAd(new AdRequest()); AdViewStarted = true; 
+5
source

All Articles