How do I make Banners Ads (admob) Common for All My Activities

隐身守侯 提交于 2019-12-22 07:59:33

问题


I have about three Activities, and all these three activities have Banner ads at the bottom which are set by code in the OnCreate() method of the Three Activities.

And due to some reason I need to Finish each Activity while moving from one activity to the other, and startActivity() for coming back to the first Activity.

I wanted to know, how do i make just one Banner Ad for all these three Activity instead of calling them individually from different onCreate, because my doubt is that on transition of Activities I am refreshing Ads(quiet ofently) which isn't a good practice for your clicks.

Should I declare it in a static Class so that it can be called from any activity and just one instance would be there(so no refreshing due to activity creation)

Suggestions are welcome.


回答1:


Do you know about implementing ViewStub?

For your problem, ViewStub is used to place AdMob ads at Footer, you just have to create layout for this Footer and then include this layout in your XML layouts (activity layouts) by using ViewStub example.

Here is an example for implementing ViewStub, yes its for Title bar but you can take concept from it.

Now, to optimize solution (code), you can create an Abstract class and extends Activity class and include your AdMob ads code inside this class.

For example:

public abstract class BaseActivity extends Activity
{
   /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

        public void setFooterAds()
        {
              // Make ViewStub visible
              // include your Ads code
        }
}

Now, you just have to extend this BaseActivity class in your Activity classes, and call setFooterAds() method to display AdMob ads.




回答2:


You can put the code like this in your main activity, so that the ads banner will be displayed in all the three activities.

import com.google.ads.*; 

public class testActivity extends Activity { 
private static final String MY_AD_UNIT_ID = "yourId"; 
private AdView adView; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    super.loadUrl("file:///android_asset/www/index.html");          
    // Create the adView 
    adView = new AdView(this, AdSize.BANNER, MY_AD_UNIT_ID); 
    LinearLayout layout = super.root; // this is the only change 
    layout.addView(adView); 
    adView.loadAd(new AdRequest());

Xml File:

<com.admob.android.ads.AdView
      android:id="@+id/ad"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      xmlns:backgroundColor="#000000"
      xmlns:primaryTextColor="#ffffff"
      xmlns:secondaryTextColor="#cccccc"



回答3:


I think the only way out here is to use single activity and multiple fragments..the activity will have a frame layout and a fragment containing Ad..While different screens (fragments) will be replaced depending on the UX, the Ad-containing-fragment will stay as it is, common to all screens!



来源:https://stackoverflow.com/questions/9629230/how-do-i-make-banners-ads-admob-common-for-all-my-activities

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!