Is the AdView automatically tied to the activity life cycle or do you have to explicitly call the pause
, resume
, destroy
events? Does it depend on the size of the AdView? I'm using banner ads.
I couldn't find a lot of code samples of other people doing this and the main Android help article doesn't mention it (they just load the ad in onCreate and don't do anything else with it).
https://developers.google.com/android/reference/com/google/android/gms/ads/AdView (code example includes pause/resume/destroy and it mentions we "should call these methods" in the method notes, but doesn't elaborate).
https://developers.google.com/admob/android/banner (does not mention the need to pause/resume/destroy).
http://thetechnocafe.com/a-complete-guide-to-integrating-admob-in-your-android-app/ (pauses and destroys video ads in the code, but doesn't mention why or give any explanation)
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- app content -->
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/myAdView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
</com.google.android.gms.ads.AdView>
</LinearLayout>
public class MainActivity extends AppCompatActivity {
private AdView mAdView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// ...
mAdView = findViewById(R.id.myAdView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
}
// do I need this code as well???
@Override
public void onResume() {
mAdView.resume();
super.onResume();
}
@Override
public void onPause() {
mAdView.pause();
super.onPause();
}
@Override
public void onDestroy() {
mAdView.destroy();
super.onDestroy();
}
Yes, we need to add these lines of code for saving amount of memory availability.
@Override
public void onResume() {
super.onResume();
mAdView.resume();
}
you move resume() of adview below super constructor.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With