ITKeyword,专注技术干货聚合推荐

注册 | 登录

android - How can I retain a PublisherAdView on an orientation change?

itPublisher 分享于

2021腾讯云限时秒杀,爆款1核2G云服务器298元/3年!(领取2860元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1062

2021阿里云最低价产品入口+领取代金券(老用户3折起),
入口地址https://www.aliyun.com/minisite/goods

推荐:Android Screen Orientation Change (Screen Rotation) Example

原文见: http://techblogon.com/android-screen-orientation-change-rotation-example/#

I'm using a PublisherAdView to display a banner ad in my app. When the app starts I create the PublisherAdView, add it to a layout, and request an ad. I would like to display the same ad that has already been requested after an orientation change. Currently the PublisherAdView gets destroyed and recreated on an orientation change, so I lose the ad that was requested.

Here is how I create the PublisherAdView in a fragment:

PublisherAdView banner_ad_view = new PublisherAdView(getActivity());
banner_ad_view.setAdUnitId(ad_unit_id);
banner_ad_view.setAdSizes(new AdSize(ad_width, ad_height));

Here is how I add the PublisherAdView to my fragment layout:

int id = R.id.fragment_banner_ad_container;
RelativeLayout ad_container = (RelativeLayout) fragment_view.findViewById(id);
ad_container.addView(banner_ad_view);

Here is how I load an ad into the PublisherAdView (using PublisherAdRequest):

banner_ad_view.loadAd(ad_request);

I have been trying to avoid using android:configChanges as I do have different resources to manage based on orientation.

Any help is much appreciated!

android android-layout android-fragments ads android-orientation
|
  this question
asked Jun 2 '14 at 21:53 Chris 1

 | 

1 Answers
1

Try this:

推荐:Android 2.3 Rotation and Orientation

Service: 28 window: [ android.view.IWindowManager] 38 activity: [ android.app.IActivityManager] 46 SurfaceFlinger: [ android.ui.ISurfaceComposer] 1) I

On android manifest in the activity that you want

android:configChanges="orientation|screenSize|keyboardHidden" >

and in the main activity:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    // Checks the orientation of the screen
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
    }
}

Thats work for me


|
  this answer
answered Jun 2 '14 at 23:40 Kakaman593 29 5

 | 

推荐:How to set Android camera orientation properly

This solution will work forallversions of Android. You can use reflection in Java to make it work for all Android devices: Basically you should create


相关阅读排行


相关内容推荐

最新文章

×

×

请激活账号

为了能正常使用评论、编辑功能及以后陆续为用户提供的其他产品,请激活账号。

您的注册邮箱: 修改

重新发送激活邮件 进入我的邮箱

如果您没有收到激活邮件,请注意检查垃圾箱。