title: Full screen ad in ViewPager
body: <p>I have ViewPager with slides. <strong>After every 5th slide I want to show full-screen ad.</strong> I'm using Flurry for ad service.I've successfully added ads, and they are working.</p>
  
  <p><strong>Problem</strong>: Ads are loading way too slow. When user switches to 5th slide, it takes around 3-4 seconds for an ad to appear.</p>
  
  <p><strong>My approach</strong>: </p>
  
  <ol>
  <li>I tried to put the fetching code in <code>onStart</code> method to load faster,
  but then ads just don't appear anymore.  </li>
  <li><p>Every time viewpager changes slide, fragment that contains the ad gets destroyed, and then in new slide it has to reconnect to Flurry. I tried to put connection in a main activity that starts the app, to connect earlier, but then connection can't be seen in fragment.</p>
  
  <pre><code>public class CardFragment extends Fragment {
  
      private final String kLogTag = "FlurryAdServingAPI";
  
      Card mCard;
      TextView mTextView;
      private ViewPager mViewPager;
      private MenuItem mAddToFavorite;
      private AudioPlayer mPlayer = new AudioPlayer();
  
      private FlurryAdInterstitial mFlurryAdInterstitial = null;
      private String mAdSpaceName = "12thcard";
      private FrameLayout mAdLayout;
  
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setHasOptionsMenu(true);
          mCard = (Card) getArguments().getSerializable(EXTRA_CARD_ID);
          GameManager.get(getActivity()).changeCardTimesDisplayed(mCard);
          mViewPager = (ViewPager) getActivity().findViewById(R.id.viewPager);
          mFlurryAdInterstitial = new FlurryAdInterstitial(getActivity(), mAdSpaceName);
          mFlurryAdInterstitial.setListener(interstitialAdListener);
  
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
          View v = inflater.inflate(R.layout.fragment_card, parent, false);
          mAdLayout = (FrameLayout)v.findViewById(R.id.bannerframe);
          if(mViewPager.getCurrentItem()%5 == 0) {
              Log.i("FluryTag", "Show ad");
              FlurryAds.fetchAd(getActivity(), mAdSpaceName, mAdLayout, FlurryAdSize.FULLSCREEN);
              FlurryAds.displayAd(getActivity(), mAdSpaceName, mAdLayout);
          }
          mTextView = (TextView) v.findViewById(R.id.questionText);
          mTextView.setText(mCard.getText());
          return v;
      }
  
      @Override
      public void onDestroy() {
          super.onDestroy();
          mPlayer.stop();
      }
  
      public void onStart() {
          super.onStart();
          try {
              Log.d(kLogTag, "onStartSession:  "+ AppConstants.FLURRY_API_KEY);
              FlurryAgent.onStartSession(getActivity(), AppConstants.FLURRY_API_KEY);
              mFlurryAdInterstitial.fetchAd();
          } catch (Exception e) {
              Log.e(kLogTag, e.getMessage());
          }
      }
      public void onStop() {
          super.onStop();
          FlurryAgent.onEndSession(getActivity());
          mFlurryAdInterstitial.destroy();
          Log.d(kLogTag, "onEndSession");
      }
  }
  </code></pre>
  
  <p>FlurryAdinterstitialListener</p>
  
  <pre><code>FlurryAdInterstitialListener interstitialAdListener = new FlurryAdInterstitialListener() {
  
          @Override
          public void onFetched(FlurryAdInterstitial adInterstitial) {
              Log.d(kLogTag, "onFetched( " + adInterstitial + " )");
              adInterstitial.displayAd();
          }
          @Override
          public void onError(FlurryAdInterstitial adInterstitial, FlurryAdErrorType adErrorType, int errorCode) {
              Log.d(kLogTag, "onError( " + adInterstitial + " )");
              Log.d(kLogTag, "onError adErrorType( " + adErrorType + " )");
              Log.d(kLogTag, "onError errorCode( " + errorCode + " )");
              adInterstitial.destroy();
          }
          //..
          //the remainder of listener callbacks
      };
  </code></pre></li>
  </ol>
  
  <p><strong>Ideas:</strong> Am i going the wrong direction? I mean the nature of ViewPager is to create and discard fragments and if every fragment needs time to connect to service, fetch the ad, and then display. Perhaps, I shouldn't be displaying ads in ViewPager. </p>