in my app I have a navigationbar with three tabs, each tab is a Fragment and the bar looks like this: https://gyazo.com/5052f885effb3e0154d407b3bd8d3884 It's simply a normal navigation bar with three tabs.
So now I want to display my Interstitial ad (which I load in my MainActivity onCreate) by pressing the second tab in this case the tab which is named "Ausraster".
So I did the following: I loaded the Interstitial ad in my MainActivity onCreate like this:
public class MainActivity extends AppCompatActivity{
public InterstitialAd mInterstitialAd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AdRequest adRequest = new AdRequest.Builder().build();
mInterstitialAd = new InterstitialAd(MainActivity.this);
mInterstitialAd.setAdUnitId("MY ID");
mInterstitialAd.loadAd(adRequest);
}
and then I wrote the displayInterstitial method in my MainActivity:
public void displayInterstitial() {
mInterstitialAd.setAdListener(new AdListener() {
public void onAdLoaded() {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
}
}
});
}
At last I wrote this line in the onCreateView of my second fragment the "Ausraster" fragment:
((MainActivity)getActivity()).displayInterstitial();
Should now actually fold everything but the problem is following: If I start the application the Interstitial Ad displays after a delay of 3 seconds without pressing on the second tab. But I want that its only display if I click on the second tab. So what can I do?
I have this problem now over a month and I would be so glad if someone can tell me why the Interstitial ad displays without pressing the second tab.
Here are all my codes, I hope you can explain it to me and sorry for my bad English :)
MainActivity:
public class MainActivity extends AppCompatActivity{
FragmentManager mFragmentManager;
FragmentTransaction mFragmentTransaction;
public InterstitialAd mInterstitialAd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/**
*Setup the NavigationView
*/
mNavigationView = (NavigationView) findViewById(R.id.shitstuff) ;
/**
* Lets inflate the very first fragment
* Here , we are inflating the TabFragment as the first Fragment
*/
mFragmentManager = getSupportFragmentManager();
mFragmentTransaction = mFragmentManager.beginTransaction();
mFragmentTransaction.replace(R.id.containerView,new TabFragment()).commit();
AdRequest adRequest = new AdRequest.Builder().build();
mInterstitialAd = new InterstitialAd(MainActivity.this);
mInterstitialAd.setAdUnitId("MyId");
mInterstitialAd.loadAd(adRequest);
}
public void displayInterstitial() {
mInterstitialAd.setAdListener(new AdListener() {
public void onAdLoaded() {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
}
}
});
}
}
TabFragment:
public class TabFragment extends Fragment {
public static TabLayout tabLayout;
public static ViewPager viewPager;
public static int int_items = 3 ;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
/**
*Inflate tab_layout and setup Views.
*/
View x = inflater.inflate(R.layout.tab_layout,null);
tabLayout = (TabLayout) x.findViewById(R.id.tabs);
viewPager = (ViewPager) x.findViewById(R.id.viewpager);
/**
*Set an Apater for the View Pager
*/
viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));
/**
* Now , this is a workaround ,
* The setupWithViewPager dose't works without the runnable .
* Maybe a Support Library Bug .
*/
tabLayout.post(new Runnable() {
@Override
public void run() {
tabLayout.setupWithViewPager(viewPager);
}
});
return x;
}
class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
/**
* Return fragment with respect to Position .
*/
@Override
public Fragment getItem(int position)
{
if(position == 0){
return new KommentareFragment();
}
if(position == 1){
return new AusrasterFragment();
}
if(position == 2){
return new LustigesFragment();
}
return null;
}
@Override
public int getCount() {
return int_items;
}
/**
* This method returns the title of the tab according to the position.
*/
@Override
public CharSequence getPageTitle(int position) {
switch (position){
case 0 :
return "Kommentare";
case 1 :
return "Ausraster";
case 2 :
return "Lustiges";
}
return null;
}
}
final public void showInterstitial(){
mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
super.onAdLoaded();
if(mInterstitialAd.isLoaded()){
mInterstitialAd.show();
}
}
@Override
public void onAdClosed() {
}
});
}
}
And at last my secondFragment:
public class AusrasterFragment extends Fragment{
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView=inflater.inflate(R.layout.ausraster_layout,container,false);
return rootView;
}
The ViewPager
loads other off-screen fragments in advance to make the animation look smooth when change the tab.
Because of this, the ads was getting loaded without clicking on the second tab. However, you can change the number of off screen fragments to load by using viewPager.setOffscreenPageLimit
but its default and minimum value is 1. If you set it to 1, your second tab will be created in advance but not the third one.
To load ads on selection of second tab, you have to use ViewPager's pageChangeListener and check if second tab got selected, show the Ads.
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
public void onPageScrollStateChanged(int state) {}
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
public void onPageSelected(int position) {
if(position == 1)
displayInterstitial();
}
});
And don't forget to remove ((MainActivity)getActivity()).displayInterstitial();
from the fragment.