max
max

Reputation: 1589

Full screen ad in ViewPager

I have ViewPager with slides. After every 5th slide I want to show full-screen ad. I'm using Flurry for ad service.I've successfully added ads, and they are working.

Problem: Ads are loading way too slow. When user switches to 5th slide, it takes around 3-4 seconds for an ad to appear.

My approach:

  1. I tried to put the fetching code in onStart method to load faster, but then ads just don't appear anymore.
  2. 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.

    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");
        }
    }
    

    FlurryAdinterstitialListener

    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
        };
    

Ideas: 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.

Upvotes: 2

Views: 771

Answers (1)

max
max

Reputation: 1589

If anyone is wondering. I wasn't able to solve the problem with Flurry. Eventually, I switched to AdMob to solve slow loading of ads.

Upvotes: 1

Related Questions