0

I'm creating an app having 2 tabs and Ad Mob, my question is how can I integrate interstitial ad in 2nd tab ? , means as per google AdMob policy Placing an interstitial ad after every user action is restricted, though I want to add fullscreen Ad when I swipe from 1st Tab to 2nd Tab and restrict the Ad when I swipe from the 2nd tab to 1st tab. How can I do that? Guide me with examples and corrections! Below is my current activity.

boolean firstResume = true;

 private ViewPager viewPager;
 private FBTabsAdapter mAdapter;
 private ActionBar actionBar;
 Handler handler1 = new Handler();
 private Context context;
 private int i = 0;
 private boolean exitAds = false;
 // private Fragment _fragment;

 // Tab titles
 private String[] tabs = { "TAB 1", " TAB 2" };

 @Override
 protected void onResume() {
  super.onResume();
  if (!firstResume) {

  } else {
   firstResume = false;
  }
 }

 @Override
 public void onTabReselected(Tab tab, FragmentTransaction ft) {
 }

 @Override
 public void onTabSelected(Tab tab, FragmentTransaction ft) {
  // on tab selected
  // show respected fragment view
  viewPager.setCurrentItem(tab.getPosition());
  if (tab.getPosition() == 1) {
   Log.e("UPDATED", "DOWNLOADS SELECTED");
   final DownloadsFragment fragment = ((DownloadsFragment) getSupportFragmentManager()
     .findFragmentByTag("android:switcher:" + R.id.pager + ":1"));
   fragment.updateDownloads();

   // if (i == 0) {
   // i++;
   // } else if (i > 0) {
   // }

  } // else
   // displayInterstitial();
 }

 @Override
 public void onTabUnselected(Tab tab, FragmentTransaction ft) {
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.main, menu);
  return true;
 }

TABS ADAPTER

package xxxxxx.xxxxxxxxxxx.xxxxxxxxx.com;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;


public class ABTabsAdapter extends FragmentPagerAdapter {
    public ABTabsAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int index) {

        switch (index) {
            case 0:
                // Top Rated fragment activity
                return new TAB1Fragment();
            case 1:
                // fragment activity
                return new TAB2Fragment();
        }

        return null;
    }

    @Override
    public int getCount() {
        // get item count - equal to number of tabs
        return 2;
    }
    
}
Agatha Jannet
  • 38
  • 1
  • 10
  • First create activity and create 2 fragments(TAB1,TAB2) and make connection to the fragment to activity.before create interstitial ads ID – Vadivel Jan 30 '17 at 11:30
  • Firstly keep the ad loaded as soon as parent container activity is created then get the context of the container activity in the page adapter and as soon as the fragment of your choice is selected show the ad.. should be simple as that :) – shadygoneinsane Jan 30 '17 at 11:32
  • @ vadivel @shadygoneinsane , hi ,can you show me some examples? – Agatha Jannet Jan 30 '17 at 11:40
  • check the position of tab in `onTabSelected` method and show the interstitial ads based on the current position of the tab – Amrut Bidri Jan 30 '17 at 11:41
  • Have you created two different fragments ? If you could the simply load your Ads in second `TAB2` fragment in `onCreateView()` method. Another way is to detect position of `TAB2` in `onTabSelected` method. @AgathaJannet – Piyush Jan 30 '17 at 11:46
  • @Amrut Bidri @ Piyush, I have updated my code please check it – Agatha Jannet Jan 30 '17 at 11:52
  • You should go through [this link](http://stackoverflow.com/q/20347665/5134647) and use `setUserVisibleHint` event to load an add.. – shadygoneinsane Jan 30 '17 at 11:54
  • @AgathaJannet My question is that if you'r calling two different fragments then load your ads in second fragment when your fragment completely load. _setUserVisibleHint_ will do your work. – Piyush Jan 30 '17 at 11:55
  • Check [http://www.truiton.com/2015/06/android-tabs-example-fragments-viewpager/](http://www.truiton.com/2015/06/android-tabs-example-fragments-viewpager/) @AgathaJannet – Piyush Jan 30 '17 at 12:03
  • @AgathaJannet have you checked link ? – Piyush Jan 30 '17 at 12:25
  • @ Piyush, I have checked it, but I didn't get anything, can you show me some example with an ad in it? I'm new to this things, though . Please check the updated tabs adapter code. – Agatha Jannet Jan 30 '17 at 18:45

0 Answers0