Reputation: 12010
I have an application in which you can open unlimited number of activities, it's like article app, every time you click on an article a new activity is created. I am using this code to load AdMob ads
AdView adView = new AdView(context);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId(Config(context, "banner_unit_id"));
AdRequest adRequest = new AdRequest.Builder().build();
if(adView.getAdSize() != null || adView.getAdUnitId() != null){
adView.loadAd(adRequest);
}
Now what I want to do is use the same loaded ad on multiple activities, so the ad won't have to be loaded again and again on every single activity. Is there any way of doing this?
Upvotes: 1
Views: 3970
Reputation: 2417
you can it just load ad in application class and use it in any activity.
actual ans
as I do it,
App class
import android.app.Application;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
public class App extends Application {
AdView adView;
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
adView = new AdView(this);
adView.setAdSize(AdSize.SMART_BANNER);
adView.setAdUnitId("ca-app-pub-1267746788642565/8418489933");
// Request for Ads
AdRequest adRequest = new AdRequest.Builder().build();
// Load ads into Banner Ads
adView.loadAd(adRequest);
}
public void loadAd(LinearLayout layAd) {
// Locate the Banner Ad in activity xml
if (adView.getParent() != null) {
ViewGroup tempVg = (ViewGroup) adView.getParent();
tempVg.removeView(adView);
}
layAd.addView(adView);
}
}
main Activity
public class MainActivity extends Activity {
App app;
LinearLayout layAd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layAd = (LinearLayout) findViewById(R.id.layad);
app = (App) getApplication();
app.loadAd(layAd);
Button btnNext = (Button) findViewById(R.id.next);
btnNext.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent iNext = new Intent(MainActivity.this,
SecondActivity.class);
startActivity(iNext);
}
});
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
app.loadAd(layAd);
super.onResume();
}
}
Second Activity
public class SecondActivity extends Activity {
App app;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
LinearLayout layAd = (LinearLayout) findViewById(R.id.layad);
app = (App) getApplication();
app.loadAd(layAd);
}
}
Manifest xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.admobdemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:name="com.example.admobdemo.App"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.admobdemo.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.admobdemo.SecondActivity"
android:label="@string/app_name" >
</activity>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity
android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
</application>
</manifest>
main activity layout xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}" >
<LinearLayout
android:id="@+id/layad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
<Button
android:id="@+id/next"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
and second activity layout xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}" >
<LinearLayout
android:id="@+id/layad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
Upvotes: 1
Reputation: 20196
You cannot do this without breaking something else.
The recommended way of achieving what you are after is to load up your content in new Fragments and keep a single Activity intact in which to display them.
Upvotes: 2