My app is using navigation drawer. Upon clicking on the item in the drawer, it will display a fragment. And I put the AdMob code inside the fragment, as displayed below:
public class MenuIncome extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.menu_income, container, false);
AdView mAdView = (AdView) rootView.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
mAdView.destroy();
return rootView;
}
}
Before I put the AdMob code, my fragment would be displayed instantaneously upon clicking an item on the navigation drawer. But after I put the AdMob code, when I click an item on the drawer, sometimes my app would be liked freezed for up to 1 second, then only the fragment (with the ad) would be displayed.
Why do this happen? I thought AdView have already load its ad asynchronously.
The first call of
loadAd(..)
takes some time for the initialization of admob and this freezes your fragment when loading. You can delay the initialization until your fragment has been loaded and is visible:Have you tried instead to place the
AdView
in your parentActivity
which is hosting theFragment
?The below setup is how I use ads in my application, which also uses a navigation drawer and multiple fragments. There is no delay at all in loading the fragments. In fact, at times the fragment will load well before the ad, depending on the case.
MainActivity.java
:activity_main.xml
: