I want to add interstitial ads to my viewPager but I am running into the issue of the container "disappearing" (or being destroyed) once the interstitial ad closes (or maybe when it opens). The @Override method "public void destroyItem(ViewGroup container, int position, Object object)" is automatically called once you swipe, and the interstitial ad is invoked, however, the view is not inflated again like it does when no interstitial ad is invoked.
My class extends PagerAdapter and my general setup is as follows:
@Override
public boolean isViewFromObject(View view, Object object) {
// TODO Auto-generated method stub
return view == ((RelativeLayout) object);
}
@Override
public Object instantiateItem(final ViewGroup container, final int position) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//instantiate TextViews, Buttons, ect (code not posted)
((ViewPager) container)
.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// the maximum position value is 130
// I want to display an interstitial ad every 7th position
if (position % 7 == 0) {
displayInterstitial();
}
}
@Override
public void onPageScrollStateChanged(int state) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrolled(int position,
float positionOffset, int positionOffsetPixels) {
// TODO Auto-generated method stub
}
});
if (interstitialLoad == false && isConnection == false) {
if (NetworkUtils.isConnected(context) == true
&& NetworkUtils.isNetworkActive(context) == true) {
isConnection = true;
// create an ad
interstitialAd = new InterstitialAd(context);
interstitialAd.setAdUnitId(AD_UNIT_ID_INT);
// request interstitial ads
AdRequest adRequestInterstitial = new AdRequest.Builder()
.build();
// begin loading interstitial
interstitialAd.loadAd(adRequestInterstitial);
}
}
if (isConnection = true) {
// Set the AdListener.
interstitialAd.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
// do nothing
}
@Override
public void onAdLoaded() {
if (interstitialLoad == false) {
interstitialLoad = true;
}
}
@Override
public void onAdFailedToLoad(int errorCode) {
String message = String.format("onAdFailedToLoad (%s)",
getErrorReason(errorCode));
Log.e(TAG, message);
}
});
}
// add fragments to container (ViewPager)
((ViewPager) container).addView(itemView);
return itemView;
}
/** Gets a string error reason from an error code. */
private String getErrorReason(int errorCode) {
String errorReason = "";
switch (errorCode) {
case AdRequest.ERROR_CODE_INTERNAL_ERROR:
errorReason = "Internal error";
break;
case AdRequest.ERROR_CODE_INVALID_REQUEST:
errorReason = "Invalid request";
break;
case AdRequest.ERROR_CODE_NETWORK_ERROR:
errorReason = "Network Error";
break;
case AdRequest.ERROR_CODE_NO_FILL:
errorReason = "No fill";
break;
}
return errorReason;
}
// invoke displayInterstitial() when ready to display an interstitial
private void displayInterstitial() {
if (interstitialLoad == true && interstitialAd.isLoaded()) {
Log.e(TAG, "displayInterstitial()");
interstitialAd.show();
}
}
@Override
public void restoreState(Parcelable state, ClassLoader loader) {
// TODO Auto-generated method stub
super.restoreState(state, loader);
}
@Override
public Parcelable saveState() {
// TODO Auto-generated method stub
return super.saveState();
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((RelativeLayout) object);
}
This is the order in which things are occurring (lets say we are on position 6 (pg 6)):
1) When I swipe to page 7, first the @Override destroyItem(ViewGroup container, int position, Object object) method is automatically called.
2) Then the @Override onPageSelected(int position) method is automatically called, and the displayInterstitial() method is called explicitly.
3) From inside the displayInterstitial() method, the interstitial ad is displayed. At this point, the @Override method saveState() is automatically called.
4) As the 'end user', I then click on the "X" or press the device back button to close the displayed ad.
5) The entire view (or/and container) is gone. The expected behavior I hoped for was that the @Override method restoreState() would be called and then I can still see my container and it's view.
How do I make it so that everything picks back up from where it left off before the interstitial was called and closed? Anyone with any ideas on how to troubleshoot this? Thanks in advance!