How to make banner ads (admob) Common to all my actions

I have three actions, and all three of these actions have banner ads at the bottom, which are set by code in the OnCreate() method for the three activities.

For some reason, I need to finish each action, moving from one activity to another, and startActivity() to return to the first activity.

I wanted to know how I can only make one banner ad for all of these three actions, instead of calling them separately from different onCreate , because I doubt that when I switch Activities, I update Ads (quietly), which is not good practice for your clicks.

Do I have to declare it in a static class so that it can be called from any activity, and only one instance would be there (so there was no update due to the creation of the activity).

Suggestions are welcome.

+4
source share
3 answers

Do you know about the implementation of ViewStub?

For your problem, ViewStub is used to place AdMob ads in Footer , you just need to create a layout for this footer, and then include this layout in your XML layouts (action layouts) using the ViewStub example.

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

Now, to optimize the solution (code), you can create an Abstract class and extend the action and include the AdMob ad class in it.

For instance:

 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 need to extend this BaseActivity class in your Activity classes and call the setFooterAds () method to display AdMob ads.

+2
source

You can put such a code in your main activity so that the ad banner is displayed in all three actions.

 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" 
+1
source

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

+1
source

Source: https://habr.com/ru/post/1400520/


All Articles