Question

I've added an AdMob banner to the first screen of the app. Now i need it on some other screens (different activities). How do I implement it without reloading banner to avoid extra usage of traffic?

Thanks.

Was it helpful?

Solution

I put Admob in its own fragment and just reuse that fragment across activities.

OTHER TIPS

For someone who want the Demo code, I implement this in my apps.

Use one Activity + multiple Fragments

  1. Create a MainActivity, this activity manage all the fragments. in layout file below, the FrameLayout is the container of your fragments(to show the actual content of your app), and the fragment is the container of the Admob Ad banner.

===

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <FrameLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@+id/adFragment"/>
        <fragment
            android:id="@+id/adFragment"
            android:name="com.jiyuzhai.wangxizhishufazidian.MainActivity$AdFragment"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true" />
</RelativeLayout>

===

  1. Add the first fragment in MainActivity, this is the first screen when you app start.

==

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .add(R.id.container, new MainFragment())
                    .commit();
        }
    }
}

==

  1. Switch the fragment when you want, and the banner will be there(at the bottom of all the screen) all the time.

==

FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container, fragment);
fragmentTransaction.addToBackStack("null");
fragmentTransaction.commit();

==

Layout of the Ad banner

==

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        ads:adSize="SMART_BANNER"
        ads:adUnitId="@string/banner_ad_unit_id">
    </com.google.android.gms.ads.AdView>
</RelativeLayout>

==

Note: Be sure you really want to do that when using this approach, for example, that's a not good user experience to show a banner at some page like Settings and About. you can easily hide/show the Ad banner by settings the visibility of the AdView to VISIBLE/INVISIBLE/GONE.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top