OnCreate中的AdMob广告可以,但是如果您返回活动状态则消失,为什么? [英] AdMob ad in onCreate OK, but disappears if you return to activity, why?

查看:85
本文介绍了OnCreate中的AdMob广告可以,但是如果您返回活动状态则消失,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将广告添加到我的活动中,如下所示:

I've added an ad to my activity like follows :

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.languageselection);

    // Create the adView
    adView = new AdView(this, AdSize.BANNER, MY_AD_UNIT_ID);

    // Lookup your LinearLayout assuming it�s been given
    // the attribute android:id="@+id/mainLayout"
    LinearLayout layout = (LinearLayout) findViewById(R.id.ad_layout);

    // Add the adView to it
    layout.addView(adView);

    // Initiate a generic request to load it with an ad
    adView.loadAd(new AdRequest());


}

加载广告的罚款。但是,如果我单击菜单按钮并修改一些共享的首选项并返回,则广告消失了,并且没有重新填充。

That loads the ad fine. However if I click the menu button and modify some shared preferences and return, the ad has disappeared and doesn't get re-filled.

这就是我从中导航的方式我的活动中,请注意我没有在活动中结束,所以在onCreate中创建的广告应该仍然存在吗?

This is how I navigate out from my activity, notice that I don't call finish on the activity, so the ad that was created in the onCreate should still be there?

@Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        switch (item.getItemId())
        {
            case R.id.aboutme:
                startActivity(new Intent(this, About.class));
                return true;
            case R.id.help:
                startActivity(new Intent(this, Help.class));
                return true;
            case R.id.preferences:
                startActivity(new Intent(this, EditPreferences.class));
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

如何确保广告不会丢失如果我离开活动并返回?

How can I ensure an ad doesn't get lost if I navigate away from the activity and return?

推荐答案

我个人在onStart()中创建了一个新的AdRequest:

Personally I create a new AdRequest in onStart():

public void onStart() {
    super.onStart();
    if(adView != null) {
        adView.loadAd(new AdRequest());
    }
}

这篇关于OnCreate中的AdMob广告可以,但是如果您返回活动状态则消失,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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