安卓:重用同一个视图对象在不同的​​活动(的情况下大约是广告横幅) [英] Android: Reusing same View object in different Activities (the case is about ad banners)

查看:228
本文介绍了安卓:重用同一个视图对象在不同的​​活动(的情况下大约是广告横幅)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想重新使用同一个视图对象(而不是视图类,或xml文件,我的意思是在内存中的对象),在不同的活动。

I want to reuse the same view object (not the view class, or the xml file, I mean the object in the memory) in different Activities.

我几乎已经完成这件事。问题是,当然,上下文。

I almost had this done. The thing is, of course, the context.

我试图与应用程序上下文。它几乎工作,但后来当我点击我的不同活动之间传输的看法,并尝试启动另一个应用程序从它(或链接)坠毁。 (我不记得确切的崩溃,但我把它挖出来,如果​​你需要的话)。

I tried with the application context. It almost worked, but then when I click on the view I am transmitting between different activities, and try to start another application from it (or link) it crashed. (I don't remember the exact crash, but I'll dig it, if you need it).

我尝试了一些活动,我并不介意,一旦泄漏,并给它一个上下文。它的工作,实际上一切工作,直到我开始变得怪异异常的ViewFlipper。

I tried with some activity, that I didn't mind leaking once, and giving it as a Context. It worked, actually everything worked, until I began to get weird exceptions in ViewFlipper.

我的问题是,这是重用真的有可能,怎么办呢稳定吗?你有与任何经验?

My question is, is this reusing really possible, and how to do it stable? Have you got any experience with that?

在此先感谢了很多, 丹

Thanks A LOT in advance, Dan

推荐答案

我记住,你能负担得起泄漏1活动,因为这是我所知道的唯一的解决办法: 声明一个静态的观点,说myAdView在你的第一次活动(在您要求的广告)。现在,您可以广告,并在每一个活动TRANSATION删除此myAdView。 Ofcource你将不得不保持独立LinearLayouts乌拉圭回合在单独的活动,在这里我们将添加广告/删除myAdView 例如。假设你是从活动A到B,然后在公司的onPause删除myAdView:

I'm keeping in mind that you can afford to leak 1 activity, as this is the only solution I know: Declare a static view, say myAdView in your 1st activity (in which you are requesting ad). Now you can ad and remove this myAdView in every activity transation. Ofcource you will have to maintain seperate LinearLayouts for ur ads in seperate activities, where we will add/remove the myAdView eg. Suppose you are going from activity A to B, then in A's onPause remove myAdView:

private LinearLayout layoutAd;
layoutAd = (LinearLayout) findViewById(R.id.layout_ad); // from A's xml
protected void onPause() {
    super.onPause();
    layoutAd.removeView(FirstActivity.adBannerView);
}

和B中的onResume添加相同(FirstActivity的)myAdView:

and in B's onResume add the same (FirstActivity's) myAdView:

private LinearLayout layoutAd;
layoutAd = (LinearLayout) findViewById(R.id.layout_ad);  // from B's xml
protected void onResume() {
      super.onResume();
      layoutAd.addView(FirstActivity.adBannerView);
}

希望这能解决您的问题,在一定程度上。

Hope this solves your problem to some extent.

这篇关于安卓:重用同一个视图对象在不同的​​活动(的情况下大约是广告横幅)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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