AdMob会延迟显示片段 [英] AdMob causes delay in displaying fragments

查看:83
本文介绍了AdMob会延迟显示片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序正在使用导航抽屉。单击抽屉中的项目后,它将显示一个片段。然后将AdMob代码放在片段中,如下所示:

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;
    }
}

在我放入AdMob代码之前,我的片段是单击导航抽屉上的项目时立即显示。但是,在我放入AdMob代码之后,当我单击抽屉上的某个项目时,有时我的应用可能会被冻结1秒,然后只会显示带有广告的片段。

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.

为什么会这样?我以为AdView已经异步加载其广告。

Why do this happen? I thought AdView have already load its ad asynchronously.

推荐答案

第一次调用 loadAd(..)需要一些时间用于admob的初始化,这会在加载时冻结您的片段。
您可以延迟初始化,直到片段被加载并可见为止。

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:


             mAdView.postDelayed(
                     new Runnable() {
                         @Override
                         public void run() {
                             mAdView.loadAd(adRequest);
                         }
                     }, 500
             );


这篇关于AdMob会延迟显示片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆