问题描述
OK, I had asked a similar question before and had got an answer but that was too general a question.
Now I've an app in which there are plenty of activities. Each activity has the same admob (AdView) layout being included in its layout file. Now the problem is when I go from one activity to the other after the first screen has finished loading the ad, the second activity still waits for another ad fetch cycle to happen [ie., it again sends an ad request and displays a new ad]. All I want to do is for my app to show the same instance of the ad in every activity. [Same instance meaning: I have a time interval based on which the ads must refresh, so a new ad request must be sent only when the time limit expires, not when user navigates from one activity to another.]
Is there anyway I can do this. I have tried the "Singleton" approach mentioned in the earlier solution but there are lot of complications cos everytime I do that, it says that the specified child already has a parent and a call to removeView on the parent is necessary.
Am I doing anything wrong (OR/AND) can anybody help me with some other solution??
My Singleton class is here:
public class CommonAdFooter {
static final CommonAdFooter commonAdFooter = new CommonAdFooter();
static AdView admobView;
LayoutInflater LInflater;
private CommonAdFooter() {
LInflater = (LayoutInflater) Constants.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
admobView = (AdView) LInflater.inflate(R.layout.ad_layout, null);
LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
admobView.setLayoutParams(lp);
}
public static AdView getAdLayout() {
return admobView;
}
}
and this is my layout file for the ads
<?xml version="1.0" encoding="utf-8"?>
<com.admob.android.ads.AdView
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:myapp="..."
android:id="@+id/ad" android:layout_alignParentBottom="true"
android:background="#C9E3F6" android:layout_width="fill_parent"
android:layout_height="wrap_content" myapp:backgroundColor="#006699"
myapp:primaryTextColor="#C9E3F6" myapp:secondaryTextColor="#C9E3F6" />
Edit: Admob API link added.
I'm not sure on the exact syntax, could you link the AdMob api?
But your getting an error because when you return the ad layout it is already attached to the previous activity. So you would need something like this:
public static AdView getAdLayout() {
admobView.removeParent(); // or similar see API
return admobView;
}
EDIT
Ah here we go: AdView JavaDoc so it herits from view and RelativeLayout great.
Try this:
public static AdView getAdLayout() {
if(admobView.getParent() != null){
admobView.detachAllViewsFromParent();
}
return admobView;
}
or
public static AdView getAdLayout() {
if(admobView.getParent() != null){
admobView.getParent().removeView(admobView);
}
return admobView;
}
The answer is in the JavaDoc just a bit of trial and error
这篇关于admob 的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!